Last active
September 14, 2015 20:00
-
-
Save bjdixon/8caa713b6d459b578eca to your computer and use it in GitHub Desktop.
partial and findIndex
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 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