Umaiz Khan
Umaiz Khan

Reputation: 1227

Angular Compare 2 arrays by filter

I have a 2 arrays i need to show only data which match with first array.

Example

first array is like

["1", "2" , "3"]

second is like

[{"name": "xyz", "id": "1"},{"name":"abc", "id": "3"}, ,{"name":"def", "id": "4"}]

result

[{"name": "xyz", "id": "1"},{"name":"abc", "id": "3"}}

I try like this but showing empty Array

console.log(this.secondArr.filter(d => d.id === firstArr));

Upvotes: 1

Views: 105

Answers (1)

pascalpuetz
pascalpuetz

Reputation: 5418

You need to compare it with each entry in the array, not the array itself:

this.secondArr.filter(d => firstArray.some(arrEntry => arrEntry === d.id))

Upvotes: 5

Related Questions