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.
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));
As you can see, the return value is true, which means the element 5
In 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("指定元素在数组中"); } })
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);
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)
var arr = ["ab","cd","ef","ab","cd"]; var str="cd"; if(arr.indexOf(str)===-1){ console.log("指定元素:"+str+" 不在数组中"); }else{ console.log("指定元素: "+str+" 在数组中"); }
var str="gh"; if(arr.indexOf(str)===-1){ console.log("指定元素:"+str+" 不在数组中"); }else{ console.log("指定元素: "+str+" 在数组中"); }
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 arrayvar arr = ["ab","cd","ef","ab","cd"]; var str="gx"; if(arr.lastIndexOf(str)===-1){ console.log("指定元素:"+str+" 不在数组中"); }else{ console.log("指定元素: "+str+" 在数组中"); }
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!