Last active
December 21, 2015 00:39
-
-
Save ChrisBuchholz/6222035 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // 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