peterschussheim
10/23/2016 - 6:33 PM

pluck

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");

pluck

This Gist was automatically created by Carbide, a free online programming environment.

You can view a live, interactive version of this Gist here.