In ruby I'm able to do the following:
myObject.map(&:name)
And I get an array composed by all the name
field for all values inside myObject
(array or object).
What's the underscore.js or lodash.js equivalent? I prefer in only one line if possible :)
Example: (in js)
_.map([{name: 'x'}, {name: 'y'}], function(obj){
//dosomething
})
In ruby I'm able to do the following:
myObject.map(&:name)
And I get an array composed by all the name
field for all values inside myObject
(array or object).
What's the underscore.js or lodash.js equivalent? I prefer in only one line if possible :)
Example: (in js)
_.map([{name: 'x'}, {name: 'y'}], function(obj){
//dosomething
})
Share
Improve this question
asked Feb 25, 2014 at 21:25
VadorequestVadorequest
18k27 gold badges129 silver badges231 bronze badges
3 Answers
Reset to default 12_.pluck([{name: 'x'}, {name: 'y'}],"name");
this will give you: ["x","y"];
see http://underscorejs.org/#pluck
For lodash users,
_.map([{'name': 'x'}, {'name': 'y'}], 'name');
// ['x', 'y']
Using pure Javascript
simply use map
let data = [{name: 'x'}, {name: 'y'}];
data.map( (item) => item.name );
Will return you ["x", "y"]
.