How to filter two different type of arrays ( remove duplicate data ) using angular 6?

Viewed 409

array1 = ["one","two"];

array2 = [ {"name":"one","id":101} , {"name":"two","id":102} , {"name":"three","id":103} , {"name":"four","id":104} ];

in the above data, array1 is a collection of string values, array2 is a collection of objects. Now how to remove array1 related values in array2. I wrote code using for loops but it was too long so any predefined methods exist in angular-6/typescript.

Output:

array2 = [ {"name":"three","id":103} , {"name":"four","id":104} ];
2 Answers

You can leverage filter and includes.

let finalArray = array2.filter(item=>!array1.includes(item.name))

You can filter with checking the index of the name in array1

const array3 = array2.filter((item) => array1.indexOf(item.name) < 0);
Related