Home >Web Front-end >Front-end Q&A >How to remove specified values from es6 array
Removal method: 1. Use the "arr.splice(arr.indexOf("specified value"),1)" statement, use indexOf() to find the position of the specified value, and then use splice() to delete based on the position. This element; 2. Use the "delete arr[arr.indexOf("value")]" statement.
The operating environment of this tutorial: Windows 7 system, ECMAScript version 6, Dell G3 computer.
Remove the specified value from the es6 array
Method 1: Use the indexOf() and splice() methods
Implementation idea:
Use indexOf() to find the position of the specified value
Use splice() to delete based on the position The element
implementation code:
var arr = ["Banana", "Orange", "Apple", "Mango"]; console.log(arr); var a = arr.indexOf("Apple"); arr.splice(a,1); console.log(arr);
Method 2: Using the indexOf() method and delete keyword
Implementation ideas:
Use indexOf() to find the location of the specified value
Use the delete keyword Delete the element based on position
delete arr[index];
Implementation code:
var arr = ["Banana", "Orange", "Apple", "Mango"]; console.log(arr); var index = arr.indexOf("Orange"); delete arr[index]; console.log(arr);
Description: After using delete to delete the element, it is time to download The marked position element will be displayed as undefined, that is, the empty element
[Related recommendations: javascript video tutorial, web front-end]
The above is the detailed content of How to remove specified values from es6 array. For more information, please follow other related articles on the PHP Chinese website!