Skip to content

Instantly share code, notes, and snippets.

@bevacqua
Created February 7, 2014 16:00
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save bevacqua/8865658 to your computer and use it in GitHub Desktop.
Save bevacqua/8865658 to your computer and use it in GitHub Desktop.
Declarative variable names make for such readable code!
function pluck (a, prop) {
return a.map(function (a) { return a[prop]; });
}
function where (a, matches) {
var of = Object.keys(matches);
return a.filter(function (a) {
return of.every(function (prop) { return a[prop] === matches[prop]; });
});
}
@bevacqua
Copy link
Author

bevacqua commented Feb 7, 2014

Basic Usage

var thing = [{ name: 'Obtuse' }, { name: 'Cool' }, { name: 'Doge' }];
var names = pluck(thing, 'name');
// ['Obtuse', 'Cool', 'Doge']

Composition

var thing = [{ name: 'Obtuse', index: 5 }, { name: 'Cool', index: 2 }, { name: 'Doge', index: Infinity }];
var index = pluck(where(thing, { name: 'Doge' }), 'index');
// <- [Infinity]

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment