Created
October 23, 2016 15:21
-
-
Save gjyoung1974/5fb9e95488858250ebed693aae8b64eb to your computer and use it in GitHub Desktop.
A function test if an element is present in an Array.
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
// A function test if an element is present in an Array. | |
// Arrays.asList(Array).contains(string); | |
function f_Is_In_Array(elem, array, i) { | |
var len; | |
var indexOf = Array.prototype.indexOf; | |
if (array) { | |
if (indexOf) { | |
return indexOf.call(array, elem, i); | |
} | |
len = array.length; | |
i = i ? i < 0 ? Math.max(0, len + i) : i : 0; | |
for (; i < len; i++) { | |
// Skip accessing in sparse arrays | |
if (i in array && array[i] === elem) { | |
return i; | |
} | |
} | |
} | |
return -1; | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment