Is there any tool to to create:
[ 'John', 'Sam', 'Marry' ]
from:
[ { name: 'John' }, { name: 'Sam' }, { name: 'Marry' } ]
?
Yeah, the map() method:
var array = [{name: 'John'}, {name: 'Sam'}, {name: 'Mary'}].map(function (val) {
return val.name;
});
or jQuery's version:
var array = jQuery.map([{name: 'John'}, {name: 'Sam'}, {name: 'Mary'}], function (val) {
return val.name;
});
var input=[ { name: 'John' }, { name: 'Sam' }, { name: 'Marry' } ];
var output=[];
$.each(input, function (index, value){
output.push(value.name);
});
Using for(...) as shown in a couple of the above answers works fine, but you also run the risk of adding members you don't want this way, or running into some errors when trying to grab the name property from a member that doesn't have that property. See: Why is using "for...in" with array iteration a bad idea?