Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Create array of values from array of objects

Is there any tool to to create:

[ 'John', 'Sam', 'Marry' ]

from:

[ { name: 'John' }, { name: 'Sam' }, { name: 'Marry' } ]

?

like image 920
Sergey Metlov Avatar asked Dec 02 '22 23:12

Sergey Metlov


1 Answers

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;
});
like image 135
Matt Avatar answered Dec 31 '22 11:12

Matt