var people = [
{name: 'John', id: [34, 44, 77]},
{name: 'Jose', id: [43, 54, 65]},
{name: 'Hanna', id: [56, 34, 98]}
];
var peopleFinder = people.filter(function(b){
return b.id === 56;
}).map(function(n2){
return n2.name;
})
console.log(peopleFinder);
How to print an name from an array of objects, by a given ID. This has to be solve without using any type of loops. Just array methods are allow to be use: .filter(), .map(), .reduce() and .find().
id
is an array you can't useb.id === 56
. Use another array method likeincludes
to make sure theid
is in the array.const peopleFinder = id => people.find(e=>e.id.includes(id))?.name;
will be fine...(?)Array::reduce()
solution, seems faster