Javascript Array inArray(comparer)

Description

Javascript Array inArray(comparer)


Array.prototype.inArray = function(comparer) { 
    for(var i=0; i < this.length; i++) { 
        if(comparer(this[i])) return true; 
    }/*from w  w w. j  a v  a 2s .  c  om*/
    return false; 
};

Javascript Array inArray(comparer)

Array.prototype.inArray = function(comparer) { 
    for(var i=0; i < this.length; i++) { 
        if(comparer(this[i])) return true; 
    }//  w  w  w .j  a va 2 s  .  c  o  m
    return false; 
}; 

Array.prototype.pushIfNotExist = function(element, comparer) { 
    if (!this.inArray(comparer)) {
        this.push(element);
    }
};

Array.prototype.pushIfNotExist = function(element) { 
    if (this.indexOf(element) === -1) {
        this.push(element);
    }
};

Javascript Array inArray(comparer)

// check if an element exists in array using a comparer function and return true/false
// comparer : function(currentElement)
Array.prototype.inArray = function(comparer) { 
    for(var i=0; i < this.length; i++) { 
        if(comparer(this[i])) return true; 
    }//  w  ww.j  a va  2  s  .com
    return false; 
}; 

// adds an element to the array if it does not already exist using a comparer 
// function
Array.prototype.pushIfNotExist = function(element, comparer) { 
    if (!this.inArray(comparer)) {
        this.push(element);
    }
};



PreviousNext

Related