Skip to content

Instantly share code, notes, and snippets.

@gjyoung1974
Created October 23, 2016 15:21
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 gjyoung1974/5fb9e95488858250ebed693aae8b64eb to your computer and use it in GitHub Desktop.
Save gjyoung1974/5fb9e95488858250ebed693aae8b64eb to your computer and use it in GitHub Desktop.
A function test if an element is present in an Array.
// 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