Skip to content

Instantly share code, notes, and snippets.

@rohozhnikoff
Last active August 29, 2015 14:07
Show Gist options
  • Save rohozhnikoff/a43cfed27c41e4e68cdc to your computer and use it in GitHub Desktop.
Save rohozhnikoff/a43cfed27c41e4e68cdc to your computer and use it in GitHub Desktop.
simle and fast finder in js-array
function findInArray(array, callback) {
for (var i = 0, element = array[i], length = array.length; i < length; i++) {
if (callback.apply(callback, [element, i, array])) return element;
}
}
@mzabriskie
Copy link

Bit of an issue with this implementation. The element is only assigned once when the loop initializes. Try assigning element as follows:

for (var i = 0, element = null, length = array.length; i < length, element = array[i]; i++) {}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment