I have one array like this one:
array1=[{value:1, label:'value1'},{value:2, label:'value2'}, {value:3, label:'value3'}]
I have a second array of integer :
array2=[1,3]
I would like to obtain this array without a loop for :
arrayResult = ['value1', 'value3']
Does someone know how to do it with javascript ?
Thanks in advance
Map the elements in
array2
to thelabel
property of the element inarray1
with the correspondingvalue
:Without comments, and in ES6:
Good answers all. If I may suggest one more alternative using
Map
as this seems to be suited to a key:value pair solution.Of course this supposes that the key:value structure of the first array will not become more complex, and could be written in the simpler form of.
Just index the first array using the _.indexBy function:
You can use
.filter
and.map
, like thisA simple for-loop should suffice for this. In the future you should seriously post some code to show what you have tried.
JSBIN