Home > Web Front-end > JS Tutorial > How to delete items (elements) from a javascript array

How to delete items (elements) from a javascript array

青灯夜游
Release: 2021-07-02 17:45:59
Original
2803 people have browsed it

Deletion method: 1. Use pop() to delete elements at the end of the array; 2. Use shift() to delete elements at the beginning of the array; 3. Use the delete operator to delete elements at the specified subscript position; 4. Use length The attribute deletes one or more elements at the end; 5. Use splice() to delete one or more elements after the specified subscript position.

How to delete items (elements) from a javascript array

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

Method 1: Use the pop() method to delete elements at the end of the array

The pop() method can delete the last element in the array and return the deleted element.

Example:

var a = [];  //定义数组,模拟空栈
console.log(a.push(1));  //入栈,栈值为[1],length为1
console.log(a.push(2));  //入栈,栈值为[1,2],length为2
console.log(a.pop());  //出栈,栈值为[1],length为1
console.log(a.push(3,4));  //入栈,栈值为[1,3,4],length为3
console.log(a.pop());  //出栈,栈值为[1,3],length为2
console.log(a.pop());  //出栈,栈值为[1],length为1
Copy after login

Method 2: Use the shift() method to delete elements at the beginning of the array

The shift() method can delete the array The first element is returned, and then all remaining elements are moved forward by 1 position to fill the gap at the head of the array. If the array is empty, shift() will do nothing and return undefined.

Example:

var a = [1,2,3,4,5];  //定义数组
for (var i in a) {  //遍历数组
    var t = a.pop();  //尾部弹出
    a.unshift(t * 10);  //头部推入,把推进的值放大10倍 
}
console.log(a);  //返回[10,20,30,40,50]
Copy after login

Method 3: Use delete operator

Use delete operator to delete the array element at the specified subscript position , the deleted elements are empty elements, and the length of the deleted array remains unchanged.

var a = [1, 2, true, "a", "b"];
delete a[0];
console.log(a);
Copy after login

Method 4: Use the length attribute

Use the length attribute to delete one or more elements at the end, or even clear the entire array. The length of the array will be dynamically updated after deleting elements.

var a = [1, 2, true, "a", "b"];
a.length = 3;
console.log(a);
Copy after login

Method 5: Use the splice() method

Use the splice() method to delete one or more array elements after the specified subscript position. This method has many parameters and many functions. The example in this section only demonstrates how it deletes array elements. The first parameter is the starting subscript position of the operation, and the second parameter specifies the number of elements to be deleted.

var a = [1,2,3,4,5];
a.splice(1,2);
console.log(a);
Copy after login

In the splice(1,2,3,4,5) method, the first parameter value 1 means starting from the second element position of array a, delete 2 elements, and then delete the array a Only 3 elements remain.

If you pass a parameter to the splice() method, the method only performs the deletion operation. The parameter value specifies the starting subscript of the deleted element (including the subscript element). The splice() method will delete all subsequent elements. element.

var a = [1,2,3,4,5]; 
a.splice(2);
console.log(a);
Copy after login

【Related recommendations: javascript learning tutorial

The above is the detailed content of How to delete items (elements) from a javascript array. 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