Home>Article> How to delete an element from js array

How to delete an element from js array

zbt
zbt Original
2023-08-02 10:00:27 18304browse

Methods to delete an element from a js array: 1. Use the splice() method; 2. Use the pop() method; 3. Use the shift() method; 4. Use the delete keyword.

How to delete an element from js array

The array in JavaScript is a very commonly used data structure that can store multiple elements. Sometimes, when processing an array, we may need to delete a specific element. This article will introduce you to several ways to delete array elements in JavaScript.

1. Use the splice() method:

The splice() method is the most commonly used method in JavaScript to delete array elements. It deletes one or more elements at a specified position from an array and returns the deleted elements. The splice() method requires two parameters: the index position of the element to be deleted and the number of elements to be deleted. Here is an example:

let fruits = ['apple', 'banana', 'orange', 'grape']; fruits.splice(1, 2); console.log(fruits); // 输出:['apple', 'grape']

In the above code, we deleted 2 elements ('banana' and 'orange') starting from index position 1.

2. Use the pop() method:

The pop() method will delete the last element of the array and return the element. This method is suitable for deleting arrays with stack characteristics. Here is an example:

let fruits = ['apple', 'banana', 'orange', 'grape']; fruits.pop(); console.log(fruits); // 输出:['apple', 'banana', 'orange']

In the above code, we delete the last element ('grape') of the array.

3. Use the shift() method:

The shift() method is similar to the pop() method, but it deletes the first element of the array, and Return the element. This method is suitable for deleting arrays with queue characteristics. Here is an example:

let fruits = ['apple', 'banana', 'orange', 'grape']; fruits.shift(); console.log(fruits); // 输出:['banana', 'orange', 'grape']

In the above code, we delete the first element ('apple') of the array.

4. Use the delete keyword:

The delete keyword is an operator in JavaScript, which can delete the element at a specified position in the array. However, when you use the delete keyword to delete array elements, the length of the array will not change, and the value of the deleted element will become undefined. Here is an example:

let fruits = ['apple', 'banana', 'orange', 'grape']; delete fruits[2]; console.log(fruits); // 输出:['apple', 'banana', undefined, 'grape']

In the above code, we delete the element ('orange') at index position 2 in the array and replace it with undefined.

It should be noted that when using the delete keyword to delete an array element, the deleted element is only marked as deleted and will not actually be deleted from the memory. Therefore, in some cases, after deleting the element, Causes some strange behavior in arrays, and it is generally not recommended to use delete to delete array elements.

The above are some common methods for deleting array elements in JavaScript. You can choose the appropriate method to delete array elements based on your specific needs. Hope this article is helpful to you!

The above is the detailed content of How to delete an element from js 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
Previous article:phpstudy Next article:phpstudy