I have 3 arrays. I want to return the elements in array1 distinct from array2 and array3. Example:
array1: ["Peter", "James", "JohnDoe", "Jackie"]
array2: ["James", "Dog"]
array3: ["JohnDoe", "Cat"]
Output: ["Peter", "Jackie"]
Here is my code snippet of filtering array2 out of array1.
const array1, array2, array3;
const result = array1.filter(
element=> !array2.some(ele => element.id === ele.id)
);
// Output: ["Peter", "JohnDoe", "Jackie"]
I can think of creating another const for filtering array3, but is there a better/elegant way to attach the filters together?
0 Replies