i= == value)", exists if true is returned."/> i= == value)", exists if true is returned.">

Home  >  Article  >  Web Front-end  >  How to determine whether it is in an array in es6

How to determine whether it is in an array in es6

青灯夜游
青灯夜游Original
2023-01-16 17:35:233220browse

Judgment method: 1. Use the includes() function to determine whether the array contains a specified value. The syntax is "arr.includes(value)". If it returns true, it exists; 2. Use the find() function. , you can find the first element that matches the addition, the syntax "arr.find(function(v){if(v==value{...}})"; 3. Use the some() function, the syntax "arr.some (i=>i===value)", it exists if true is returned.

How to determine whether it is in an array in es6

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

es6 method to determine whether the specified value is in the array

In es6, you can use includes() and find() , some() method to determine whether the element is in the array.

Method 1: Use the includes() method

includes() method is used to determine whether an array contains A specified value, returns true or false. Syntax:

array.includes(searchElement, fromIndex);
  • searchElement: the element to be found;

  • fromIndex: the index to start the search Position.

Example:

arr = [1,2,3,4,5]
console.log(arr.includes(5));

How to determine whether it is in an array in es6

As you can see, the return value is true, which means the element 5In the array.

Method 2: Use the find() method

The find() method returns the first element of the array that passes the test (judged within the function) The value.

The find() method calls a function execution once for each element in the array:

  • When the element in the array returns true when testing the condition , find() returns elements that meet the conditions, and the execution function will not be called for subsequent values.

  • If there are no elements that meet the conditions, undefined is returned

Example:

var arr = [1,2,3,4,5]
arr.find(function(value){
	if(value==5){
		console.log("指定元素在数组中");
	}
})

How to determine whether it is in an array in es6

Method 3: Use some() method

some() method is used to detect whether If there is an element that meets the specified conditions, it will return true if it exists, and false if it does not exist.

arr = [1,2,3,4,5];
let istrue= arr.some(item => item === 45);
console.log(istrue);

How to determine whether it is in an array in es6

As you can see, if the return value is false, it means that the element is not in the array.

Extended knowledge: es5 method to determine whether the specified value is in the array

##In es5, you can use indexOf() and lastIndexOf() Function to determine whether an element is in an array. The

indexOf() and lastIndexOf() methods can retrieve array elements and return the index position of the specified element; if the specified element does not exist, "

-1" is returned.

1. Use indexOf() to find elements in the array.

indexOf() returns the index of the first matching item of an element value in the array. If If the specified value is not found, -1 is returned. The usage is as follows:

array.indexOf(item,start)

  • item Required. The element to find.

  • #start Optional integer parameter. Specifies the position in the array to start searching. Its legal values ​​are 0 to stringObject.length - 1. If this parameter is omitted, the search will start from the first character of the string. The

#indexOf() method performs a search in ascending index order, that is, retrieval from left to right. When retrieving, the array elements will be compared congruently with the searchElement parameter value ===.

Example: Find whether the element is in the array

var arr = ["ab","cd","ef","ab","cd"];
var str="cd";
if(arr.indexOf(str)===-1){
	console.log("指定元素:"+str+" 不在数组中");
}else{
	console.log("指定元素: "+str+" 在数组中");
}

How to determine whether it is in an array in es6

Modify the value you need to find:

var str="gh";
if(arr.indexOf(str)===-1){
	console.log("指定元素:"+str+" 不在数组中");
}else{
	console.log("指定元素: "+str+" 在数组中");
}

How to determine whether it is in an array in es6

2. Use lastIndexOf() to find elements in the array

indexOf() returns the index of the last matching item of an element value in the array, if the specified value is not found value, returns -1. Its usage is the same as indexOf().

Example: Find whether an element is in an array

var arr = ["ab","cd","ef","ab","cd"];
var str="gx";
if(arr.lastIndexOf(str)===-1){
	console.log("指定元素:"+str+" 不在数组中");
}else{
	console.log("指定元素: "+str+" 在数组中");
}

How to determine whether it is in an array in es6

[Related recommendations:

javascript learning tutorial]

The above is the detailed content of How to determine whether it is in an array in es6. 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