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
Release: 2021-04-02 13:39:17
Original
3040 people have browsed it

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(); // 删除最后一个元素
Copy after login

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, ' '));
Copy after login

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!

Related labels:
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
Latest Issues
What are JavaScript hook functions?
From 1970-01-01 08:00:00
0
0
0
What is JavaScript garbage collection?
From 1970-01-01 08:00:00
0
0
0
c++ calls javascript
From 1970-01-01 08:00:00
0
0
0
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template