Home  >  Article  >  What are the methods to delete array elements in JavaScript?

What are the methods to delete array elements in JavaScript?

百草
百草Original
2023-12-07 14:42:38985browse

Methods to delete array elements in JavaScript: 1. splice() method; 2. filter() method; 3. delete operator; 4. pop() method; 5. shift() method; 6. Use the length attribute. Detailed introduction: 1. The splice() method can add or delete elements of the array at any position. It accepts two parameters, the index of the array to be changed and the number of elements to be deleted; 2. The filter() method creates a new array, elements in a new array, etc.

What are the methods to delete array elements in JavaScript?

#In JavaScript, there are multiple ways to delete elements from an array. The following are some commonly used methods:

1. splice() method:

The splice() method can add or delete elements of the array at any position. It accepts two parameters: the index of the array to start changing (starting at 0) and the number of elements to remove.

For example, to remove 3 elements starting at index 2, you would do this:

let arr = [1, 2, 3, 4, 5];  
arr.splice(2, 3); // 删除元素 3, 4, 5  
console.log(arr); // 输出: [1, 2]

You can also use the splice() method to add elements. For example, if you want to add 3 elements starting at index 2 (for example, 7, 8, 9), you can do this:

let arr = [1, 2, 3, 4, 5];  
arr.splice(2, 0, 7, 8, 9); // 添加元素 7, 8, 9  
console.log(arr); // 输出: [1, 2, 7, 8, 9]

2, filter() method:

The filter() method creates a new array. The elements in the new array are checked by checking all elements in the specified array that meet the conditions.

For example, if you want to delete all even numbers in the array, you can do this:

let arr = [1, 2, 3, 4, 5];  
arr = arr.filter(item => item % 2 !== 0); // 删除所有偶数  
console.log(arr); // 输出: [1, 3, 5]

3. delete operator:

delete operation will delete the last element of the array. However, it does not change the length of the array. Therefore, if you try to access the deleted element, you will get undefined.

For example:

let arr = [1, 2, 3, 4, 5];  
delete arr[2]; // 删除元素 3  
console.log(arr[2]); // 输出: undefined

It should be noted that using the delete operator multiple times will destroy the continuity of the array, causing subsequent elements to move forward to fill the gaps. Therefore, it is generally not recommended to use the delete operator to delete elements from an array.

4. pop() method:

The pop() method deletes and returns the last element of the array. Similar to the delete operator, it does not change the length of the array. Therefore, if you try to access the deleted element, you will get undefined.

For example:

let arr = [1, 2, 3, 4, 5];  
arr.pop(); // 删除元素 5  
console.log(arr[4]); // 输出: undefined

Similar to the delete operator, using the pop() method multiple times will destroy the continuity of the array.

5. shift() method:

The shift() method deletes and returns the first element of the array. Similar to the pop() method, it does not change the length of the array. Therefore, if you try to access the deleted element, you will get undefined. In addition, similar to the pop() method, using the shift() method multiple times will destroy the continuity of the array.

6. Use the length property:

You can truncate the array by setting the length property of the array. For example:

let arr = [1, 2, 3, 4, 5];  
arr.length = 3; // 删除元素 4, 5  
console.log(arr); // 输出: [1, 2, 3]

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