How does es6 detect if there is an element in an array
This article mainly introduces the relevant knowledge of "how es6 detects whether there is an element in the array". The editor shows you the operation process through an actual case. The method of operation is simple and fast, and it is practical. I hope this article "how to detect an element in an array by es6" can help you solve the problem.
Detection methods: 1, with the "arr.indexOf (" element value ")" statement, if the return element subscript exists; 2, with the "arr.includes (" value ")", if there is a return true;3, use the for loop statement to traverse the array, use the if statement and the "=" operator to determine whether the array element is the specified value.
The operating environment of this tutorial: windows7 system, javascript1.8.5 version, Dell G3 computer.
Es6 detects whether there is an element in the array
Method 1: array.indexOf
This method determines whether a value exists in the array, and if so, returns the subscript of the array element, otherwise-1.
Var arr=; var index=arr.indexOf (3); console.log (index)
Method 2: array.includes (searcElement [, fromIndex])
This method determines whether a value exists in the array, and returns true if so, false otherwise
Var arr=; if (arr.includes (3)) console.log ("exist"); else console.log ("not exist")
Method 3: use for loop and if
Var arr= [1, 2, 3, 3, 4]; var kicking 0 for (var item0, 10)