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

How to delete items (elements) from a javascript array

青灯夜游
青灯夜游Original
2021-07-02 17:30:522705browse

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

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]

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);

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);

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);

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);

【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!

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