Reputation: 1347
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
Reputation: 11243
You can leverage
filter
andincludes
.
let finalArray = array2.filter(item=>!array1.includes(item.name))
Upvotes: 5
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