Check if an array contains an element in JavaScript

JavaScript is unfortunately not implemented all the methods we have available from C # or VB.NET. One is the method «Contains» from the lists. With this code you can extend the class «Array» of JavaScript to implement this method:

Array.prototype.contains = function(element) {
    for (var i = 0; i < this.length; i++) {
        if (this[i] == element) {
            return true;
        }
    }
    return false;
}

Deja una respuesta

Tu dirección de correo electrónico no será publicada. Los campos obligatorios están marcados con *

Este sitio usa Akismet para reducir el spam. Aprende cómo se procesan los datos de tus comentarios.