Skip to content

Instantly share code, notes, and snippets.

@ChrisBuchholz
Last active December 21, 2015 00:39
Show Gist options
  • Save ChrisBuchholz/6222035 to your computer and use it in GitHub Desktop.
Save ChrisBuchholz/6222035 to your computer and use it in GitHub Desktop.
// Library method --------------------------------------------------------------
var pluckWith = function(k, a) {
return function(v) {
var res = [];
for (var i = 0; i < a.length; i++)
if (a[i].hasOwnProperty(k) && a[i][k] === v)
res.push(a[i]);
return res;
};
};
// Use case --------------------------------------------------------------------
// lightweight database ;) ;)
var db = [
{name: 'dumbo', age: 12},
{name: 'dumbo', age: 12},
{name: 'jasper', age: 12},
{name: 'carlos', age: 24}];
// search methods
var getByName = pluckWith('name', db),
getByAge = pluckWith('age', db);
// do the searching whenever you need!
var peopleNamedCarlos = getByName('carlos'),
peopeWhoAre12YearsOld = getByAge(12);
console.log(peopleNamedCarlos);
console.log(peopeWhoAre12YearsOld);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment