Skip to content

Instantly share code, notes, and snippets.

@jamc92
Created February 22, 2015 18:24
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 jamc92/6f8925b4ac994e5f3556 to your computer and use it in GitHub Desktop.
Save jamc92/6f8925b4ac994e5f3556 to your computer and use it in GitHub Desktop.
JS - inArray prototype to a function
<!DOCTYPE html>
<html>
<head>
<title>in_array in JS</title>
</head>
<body>
<script>
//Se asigna la funcion agregandole una propiedad de arreglo al objeto.
Array.prototype.inArray = function() {
//para la variable en este caso
for (var j in this) {
//Si la variable en su posicion es igual a los argumentos en su posicion cero, devuelve verdadero, sino, falso.
if (this[j] == arguments[0]) {
return true;
}
}
return false;
}
//Se declara el arreglo y sus elementos
var frutas = ["Naranja", "Cambur", "Apple"];
//Se valida el arreglo con la funcion en busqueda de un elemento interno
if (frutas.inArray("Naranja")) {
//Si lo tiene imprime la siguiente linea
document.write("Mi fruta favorita es la naranja");
//Si no lo tiene imprime la siguiente linea
} else {
document.write("Si no hay naranja me quedo con la patilla");
}
//en jQuery
$.inArray("Apple", frutas);
//do
</script>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment