Home > Web Front-end > JS Tutorial > How to delete array elements in JavaScript

How to delete array elements in JavaScript

百草
Release: 2023-12-07 11:16:55
Original
1079 people have browsed it

Methods to delete array elements in JavaScript: 1. splice() method; 2. filter() method; 3. delete keyword; 4. pop() and shift() methods. Detailed introduction: 1. The splice() method is used to add and delete elements in the array. Through it, you can specify the index of the deleted element and the number of elements to be deleted; 2. The filter() method creates a new array. The elements in the array are checked by checking all elements in the specified array that meet the conditions, etc.

How to delete array elements in JavaScript

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

1. splice() method

The splice() method is used to add/remove elements in an array. With it, you can specify the index of the deleted element, as well as the number of elements to be deleted.

For example, to remove an element from the beginning of the array, you would do:

let arr = [1, 2, 3, 4, 5];  
arr.splice(0, 1); // 删除索引为0的元素,即1  
console.log(arr); // 输出:[2, 3, 4, 5]
Copy after login

If you do not provide a second argument, splice() will remove elements from the specified index to the end of the array. All elements.

let arr = [1, 2, 3, 4, 5];  
arr.splice(2); // 删除索引为2到数组末尾的所有元素,即3, 4, 5  
console.log(arr); // 输出:[1, 2]
Copy after login

2. filter() method

The filter() method creates a new array. The elements in the new array are determined by checking all elements in the specified array that meet the conditions. . This means you can use it to remove elements from an array that you don't want.

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

let arr = [1, 2, 3, 4, 5];  
arr = arr.filter(num => num % 2 !== 0); // 只保留奇数  
console.log(arr); // 输出:[1, 3, 5]
Copy after login

3. delete keyword

You can use the delete key word to delete elements from an array. But please note that delete does not change the length of the array, but only moves the following elements forward.

For example:

let arr = [1, 2, 3, 4, 5];  
delete arr[1]; // 删除索引为1的元素,即2  
console.log(arr); // 输出:[1, undefined, 3, 4, 5],注意2后面的元素都向前移动了一位
Copy after login

4, pop() and shift() methods

The pop() method is used to delete and return the last element of the array element, and the shift() method is used to remove and return the first element of the array. Both methods change the length of the array.

For example:

let arr = [1, 2, 3, 4, 5];  
arr.pop(); // 删除并返回数组的最后一个元素,即5  
console.log(arr); // 输出:[1, 2, 3, 4]
Copy after login

and:

let arr = [1, 2, 3, 4, 5];  
arr.shift(); // 删除并返回数组的第一个元素,即1  
console.log(arr); // 输出:[2, 3, 4, 5]
Copy after login

These are common ways to remove array elements in JavaScript. Depending on your specific needs and situation, you can choose the method that works best for you.

The above is the detailed content of How to delete array elements in JavaScript. For more information, please follow other related articles on the PHP Chinese website!

source:php.cn
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template