Created
October 13, 2015 15:47
-
-
Save bjdixon/8054bec55c3baa95d7a7 to your computer and use it in GitHub Desktop.
returns first key that passes predicate
This file contains 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
function findKey(obj, predicate) { | |
for (var prop in obj) { | |
if(obj.hasOwnProperty( prop ) ) { | |
if (predicate(obj[prop])) { | |
return prop; | |
} | |
} | |
} | |
} | |
function partial (fn) { | |
var initialArgs = Array.prototype.slice.call(arguments, 1); | |
return function () { | |
var remainingArgs = Array.prototype.slice.call(arguments); | |
return fn.apply({}, initialArgs.concat(remainingArgs)); | |
} | |
} | |
function compare(id, obj) { | |
return id === obj.id; | |
} | |
//usage | |
objArr = { | |
hello: {name: 'a', id:1}, | |
goodbye: {name: 'b', id:2}, | |
yahoo: {name: 'c', id:3} | |
}; | |
console.log(findKey(objArr, partial(compare, 2))); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment