I am new to ES6 features and trying to apply filter logic in my code. My current code with forEach works but I am applying filter something like below:
export function filterContactList(itemArray: any[]) {
const list = [];
if (itemArray.length > 0) {
itemArray.forEach(item => {
if (contactIds.includes(item.id)) {
list.push(item);
}
});
}
return list;
}
// what I am trying to replace with:
itemArray(item => contactIds.includes(item.id))
You could filter the array.
return itemArray.filter(item => contactIds.includes(item.id));
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With