Home >Web Front-end >Front-end Q&A >How to remove specific numbers from es6 array

How to remove specific numbers from es6 array

青灯夜游
青灯夜游Original
2022-05-05 17:36:161784browse

es6 Method to remove specific numbers from an array: 1. Use the indexOf() function to obtain the index of a specific number, using the syntax "array object.indexOf(value)"; 2. Use the splice() function to obtain Delete elements with the index value, syntax "array object.splice(index, 1)".

How to remove specific numbers from es6 array

The operating environment of this tutorial: Windows 7 system, ECMAScript version 6, Dell G3 computer.

How to remove specific numbers from es6 array

Step 1. Use indexOf() to get the index of a specific number

The indexOf() method returns the position of a specified element in the array.

This method will retrieve the array from beginning to end to see if it contains the corresponding element. The position to start retrieval is at array start or at the beginning of the array (when no start parameter is specified). If an item is found, the position of the first occurrence of item is returned. The index of the starting position is 0.

If the specified element is not found in the array, -1 is returned.

Example: Find the "39" element in the array

var arr = [31,33,35,37,39,41,43,45,57,49,51,53];
//查找数组中的 "39" 元素
var index = arr.indexOf(39);
console.log(index);

How to remove specific numbers from es6 array

Step 2. According to the obtained index value, Use splice(index,1) to delete the element

index represents the subscript position of the array element, and 1 represents the deleted element.

var arr = [31,33,35,37,39,41,43,45,57,49,51,53];
console.log(arr);
//查找数组中的 "39" 元素
var index = arr.indexOf(39);
console.log("指定元素的索引:"+index);
//删除该元素
arr.splice(index, 1);
console.log(arr);

How to remove specific numbers from es6 array

[Related recommendations: javascript video tutorial, web front-end

The above is the detailed content of How to remove specific numbers 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