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

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} ];

Upvotes: 2

Views: 550

Answers (2)

Sunil
Sunil

Reputation: 11243

You can leverage filter and includes.

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

Upvotes: 5

Yousef khan
Yousef khan

Reputation: 3204

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

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

Upvotes: 3

Related Questions