Nonlinear Programming Methods

    plck

    1.0.0 • Public • Published

    Build Status

    Tiny utility to extract a property from a list of objects

    Example:

    var pluck = require('plck');
     
    var list = [
      { foo: 23, bar: 'bar' },
      { foo: 42, bar: 'baz' }
    ];
     
    console.log(pluck('foo', list));

    Output:

    [ 23, 42 ]
     

    The second argument can be any array-like object. In case something else is passed an empty array is returned (which is the default behavior of Array.prototype.map when it is called with something that does not quack like an array).

    This is the actual implementation:

    module.exports = function plck(prop, list) {
      if (!list) return [];
      return Array.prototype.map.call(list, function(obj) {
        return obj[prop];
      });
    };

    About

    This package has been written to accompany utilities like flatten as alternative to full-blown libraries like underscore or lodash.

    Silly package name mnemonic: Think of pluck with all vowels being plucked.

    License

    MIT

    Keywords

    Install

    npm i plck

    DownloadsWeekly Downloads

    0

    Version

    1.0.0

    License

    MIT

    Last publish

    Collaborators

    • fgnass