pluck
const items = [
{
active: true,
firstName: 'Peter',
lastName: 'Schussheim'
},
{
active: true,
firstName: 'Nicky',
lastName: 'Cat'
},
{
active: false,
firstName: 'Badger',
lastName: 'Cat'
}
];
function pluck(collection, key) { ///Pluck takes an array of objects and returns an array of the values of a specific prop in it.
return collection.map(function(el) {
return el[key]; ///All we are doing is mapping a specific named prop to a new array.
});
}
var pluckFirstName = pluck(items, "firstName");
var pluckLastNames = pluck(items, "lastName");
This Gist was automatically created by Carbide, a free online programming environment.