Home >Web Front-end >Front-end Q&A >How to remove specified values ​​from es6 array

How to remove specified values ​​from es6 array

青灯夜游
青灯夜游Original
2022-04-15 18:58:591787browse

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.

How to remove specified values ​​from es6 array

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);

How to remove specified values ​​from es6 array

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);

How to remove specified values ​​from es6 array

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!

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