JS delete a certain element in the array

A first array of objects may be defined as a function of JS, used to find the position of the specified element in the array, i.e. the index, code:

Array.prototype.indexOf = function(val) { 
    for (var i = 0; i < this.length; i++) { 
        if (this[i] == val) return i; 
    } 
    return -1; 
};    

Then by getting this element of the index, using its own inherent function js array to delete this element, the code is:

Array.prototype.remove = function(val) { 
    var index = this.indexOf(val); 
        if (index > -1) { 
            this.splice(index, 1); 
        } 
};

For example, we have an array:

var arr=['aa','ss','dd','ff'];

If you want to delete the 'dd', you can use

arr.remove('dd');

 

Guess you like

Origin www.cnblogs.com/wangyongx/p/11425865.html