1

I have an array that looks like this:

var array = [{name:"AName1", value: 1},{name:"AName2", value: 2}, ...];

How do I get all the values from a specific property? Say, I want to get all the names from every object in the array, creating a new array with those names ["AName1, "AName2", ...]

I've tried to use _.pick from underscore.js:

var result = _.map(array, function (current) {
    return _.pick(current, 'Name');
});

but it creates another array of objects with only the name property, which is not what i want to do

Any help is appreciated, thanks!

Carlos Martinez
  • 4,350
  • 5
  • 32
  • 62
  • 1
    why not just iterate over array items, and for each item push the name property value to another array: `if(item.name) { result.push( item.name ); }` – gp. Aug 23 '15 at 04:05
  • 1
    `array.map(function(obj){ return {name: obj.name}; })` – amdixon Aug 23 '15 at 04:05
  • Thanks, I don't know what i was thinking, this worked for me: `array.map(function(item){ return item.name; });` – Carlos Martinez Aug 23 '15 at 04:14

1 Answers1

4

Using map like this:

array.map(function(item){ return item.name; });

The map() method creates a new array with the results of calling a provided function on every element in this array.

CD..
  • 72,281
  • 25
  • 154
  • 163