i===value)", if it returns true, it exists."/> i===value)", if it returns true, it exists.">
Home >Web Front-end >Front-end Q&A >How to determine whether an element is in an array in es6
## The operating environment of this tutorial: Windows 7 system, ECMAScript version 6, Dell G3 computer. In es6, you can use the includes, find, and some methods to determine whether an element is in an array. Here is a detailed introduction.Judgment method: 1. Use "arr.includes(value)", if it returns true, it exists; 2. Use "arr.find(function(v){if(v==value{//true }})" statement; 3. Use "arr.some(i=>i===value)", if it returns true, it exists.
Method 1: Use the includes method of es6
includes() method is used to determine whether an array contains a specified value and returns true or false. Syntax:array.includes(searchElement, fromIndex);
arr = [1,2,3,4,5] console.log(arr.includes(5));You can see that the return value is true, which means that element
5 is in the array.
Method 2 : Use the find method of es6
find() method returns the value of the first element of the array that passes the test (judged within the function).find() method is in the array Each element of Call the execution function.var arr = [1,2,3,4,5] arr.find(function(value){ if(value==5){ console.log("指定元素在数组中"); } })
arr = [1,2,3,4,5]; let istrue= arr.some(item => item === 45); console.log(istrue);
You can see that the return value is false, which means the element is not in the array.
[Related recommendations:javascript video tutorial
、web front end
】The above is the detailed content of How to determine whether an element is in an array in es6. For more information, please follow other related articles on the PHP Chinese website!