Home >Web Front-end >JS Tutorial >What are the methods to delete arrays in JavaScript?

What are the methods to delete arrays in JavaScript?

coldplay.xixi
coldplay.xixiOriginal
2021-04-02 13:39:173005browse

JavaScript array deletion method: 1. Delete by index, the code is [someArray.shift()]; 2. Delete by condition, the code is [johnRemoved = someArray.filter(function(el)].

What are the methods to delete arrays in JavaScript?

The operating environment of this tutorial: windows7 system, javascript version 1.8.5, DELL G3 computer.

How to delete JavaScript arrays:

Delete by index

//1
someArray.shift(); // 删除第一个元素
//2
someArray = someArray.slice(1); // 删除第一个元素
//3
someArray.splice(0,1); // 删除第一个元素
//4
someArray.pop(); // 删除最后一个元素

Delete by condition

someArray = [{name:"Kristian", lines:"2,5,10"},
             {name:"John", lines:"1,19,26,96"},
             {name:"Brian",lines:"3,9,62,36" }];
johnRemoved = someArray.filter(function(el) {
    return el.name !== "John";
});
console.log(JSON.stringify(johnRemoved, null, ' '));

Related free learning recommendations :javascript video tutorial

The above is the detailed content of What are the methods to delete arrays 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