Reputation: 17467
I have an array of people objects (20 in all) from a json file
{
"id": "1",
"departments": "1",
"name": "Jim Smith",
},
I want to filter ids 1, 5, and 10
let values = [1,5,10];
let filtered =this.people.filter(function(person) {
return values.some(function(val) { return person.id === val });
})
console.log(filtered);
I keep getting a blank value in filtered, where am I going wrong? It looks close.
Upvotes: 0
Views: 41
Reputation: 3
It's because the value is integer, while the persion.id is String.
you can use
return person.id === val.toString()
or
return person.id == val
And I prefer:
let filtered2 = this.people.filter(function(person) {
return person.id in values;
})
console.log(filtered2);
Upvotes: 0
Reputation: 55623
Loose typing rules:
"1" == 1
"1" !== 1
Changing your ===
to a ==
is one way to fix it.
Upvotes: 2