Home  >  Article  >  Web Front-end  >  How to implement code to delete specified elements of an array in javascript

How to implement code to delete specified elements of an array in javascript

不言
不言Original
2018-08-27 10:09:141251browse

This article brings you the code about how to delete specified elements of an array in JavaScript. It has certain reference value. Friends in need can refer to it. I hope it will be helpful to you.

Array.prototype.indexOf = function(val) {
    for (var i = 0; i < this.length; i++) {
        if (this[i] == val) return i;
    }
    return -1;
};
//返回指定元素的索引值

Array.prototype.remove = function(val) {
    var index = this.indexOf(val);
    if (index > -1) {
        this.splice(index, 1);
    }
};
//从数组中删除指定元素

使用方法
var arr = [1,2,3]
arr.remove('1') // arr == [2,3]

Related recommendations:                                                                                                                                                                                                           ‐ ‐ ‐ ‐ ​ ​ ​ ​ ​ ​ ​ ​ ​

How to delete the checked rows in the table in javascript (code)

JS array to delete the specified element

How to delete an element in an array in js

The above is the detailed content of How to implement code to delete specified elements of an array in javascript. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn