js - delete the element corresponding to a certain value in the array (filter)

the code

You can use the Array.filter() method to filter out elements not equal to 2 to obtain a new array.

The sample code is as follows:

let arr = [1, 2, 3, 2, 4, 5];
arr = arr.filter(item => item !== 2); // 过滤掉所有等于2的元素
console.log(arr); // 输出:[1, 3, 4, 5]

In the above example, we first defined an array arr and the value 2 of the element to be deleted. Then call the filter() method to filter out all the elements in the array that are not equal to 2, store them in a new array, and assign the new array to the original array arr. Finally, print arr to see the result after the qualified elements are deleted.

Guess you like

Origin blog.csdn.net/xulihua_75/article/details/130292900