Skip to content

Instantly share code, notes, and snippets.

@bjdixon
Created October 13, 2015 15:47
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save bjdixon/8054bec55c3baa95d7a7 to your computer and use it in GitHub Desktop.
Save bjdixon/8054bec55c3baa95d7a7 to your computer and use it in GitHub Desktop.
returns first key that passes predicate
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