Skip to content

Instantly share code, notes, and snippets.

@bjdixon
Last active September 14, 2015 20:00
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/8caa713b6d459b578eca to your computer and use it in GitHub Desktop.
Save bjdixon/8caa713b6d459b578eca to your computer and use it in GitHub Desktop.
partial and findIndex
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 findIndex(arr, predicate) {
var i;
for (i = 0; i < arr.length; i += 1) {
if (predicate(arr[i])) {
return i;
}
}
return -1;
}
// example use
function compare(id, obj) {
return id === obj.id;
}
objArr = [
{name: 'a', id:1},
{name: 'b', id:2},
{name: 'c', id:3}
];
alert(findIndex(objArr, partial(compare, 3)));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment