Home > Web Front-end > JS Tutorial > body text

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

不言
Release: 2018-08-27 10:09:14
Original
1254 people have browsed it

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]
Copy after login

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!

Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template