JavaScript deletes the specified element in the array (5 methods)

JavaScript deletes the specified element in the array

In JavaScript, an array is a common data type that can store multiple elements. Sometimes, we need to delete some specific elements from the array. This article describes how to delete specified elements in an array using JavaScript.

1. Use the splice() method to delete elements

The splice() method in JavaScript can be used to add or remove elements from an array. If we need to delete elements in the array, we can use the splice() method. This method accepts two parameters, the first parameter specifies the position of the element to be deleted, and the second parameter specifies the number of elements to be deleted. For example, we can delete the second element in the array with the following code:

let myArray = ["apple", "banana", "orange", "grape"];
myArray.splice(1, 1);
console.log(myArray);

The output is: ["apple", "orange", "grape"]

2. Use the filter() method to delete elements

In addition to using the splice() method, we can also use the filter() method to remove elements from the array. This method can be used to create a new array containing elements matching certain criteria. We can delete all "banana" elements in the array with the following code:

let myArray = ["apple", "banana", "orange", "grape"];
myArray = myArray.filter(function(item) {
  return item !== "banana"
});
console.log(myArray);

The output is: ["apple", "orange", "grape"]

3. Use the pop() and shift() methods to delete elements

The pop() and shift() methods can be used to remove the last and first element of the array. These methods can be used in combination with the indexOf() method if we want to delete a specific element in the array. For example, the following code deletes the second element in the array:

let myArray = ["apple", "banana", "orange", "grape"];
let index = myArray.indexOf("banana");
if (index !== -1) {
  myArray.splice(index, 1);
}
console.log(myArray);

The output is: ["apple", "orange", "grape"]

4. Use the slice() method to delete elements

The slice() method is a pure function that does not mutate the original array, but returns a new array that contains elements from start to end (end not included). We can delete the second element in the array with the following code:

let myArray = ["apple", "banana", "orange", "grape"];
let newArray = myArray.slice(0, 1).concat(myArray.slice(2));
console.log(newArray);

The output is: ["apple", "orange", "grape"]

5. Use ES6's filter() method to delete elements

The filter() method in ES6 can also be used to delete elements in an array. We can delete all "banana" elements in the array with the following code:

let myArray = ["apple", "banana", "orange", "grape"];
myArray = myArray.filter(item => item !== "banana");
console.log(myArray);

The output is: ["apple", "orange", "grape"]

Summarize

The above are the multiple ways JavaScript deletes the specified element in the array. We can choose a method that suits us to delete elements in the array according to our needs. Hope this article can help you get a better grasp of array manipulation in JavaScript.

Guess you like

Origin blog.csdn.net/achen0511/article/details/130642796