Vincent Q Nocum Vincent Q Nocum - 11 months ago 35
Javascript Question

Filter an array of objects and extract its property by using an array of keys

im having this problem which i cant wrap around my head,

much better if put in a code.

//Array of objects sample
var objects = [{id:1, name:'test1'}, {id:2, name:'test2'}, {id:3, name: 'test3'}, {id:4, name:'test4'}];
var arrayOfKeys = [3,1,4];

//extract object name property if its id property is equivalent to one of arrayOfKeys [3,1]
//var arrayOfKeys = [3,1,4];
//output sample: extractedName=['test3','test1','test4'];

i've been using filter and map but no avail, also tried nesting filter inside map im getting an arrays of array and inside is a single object.

Answer Source

You could map the objects and ren map the wanted keys for getting the name.

var objects = [{ id: 1, name: 'test1' }, { id: 2, name: 'test2' }, { id: 3, name: 'test3' }],
    arrayOfKeys = [3, 1],
    result = => id => map.get(id).name)(new Map( => [, o]))));

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download