Home >Web Front-end >JS Tutorial >How to determine whether an array contains a certain value in es6

How to determine whether an array contains a certain value in es6

青灯夜游
青灯夜游Original
2022-03-07 18:35:3110545browse

In es6, you can use the includes() method of the array to determine whether the array contains a certain value. This method can be used to detect whether the array contains a certain value. The syntax is "array object.includes( value)".

How to determine whether an array contains a certain value in es6

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

In ES5, Array already provides indexOf to find the position of an element. If it does not exist, it returns -1. However, this function has two small shortcomings when determining whether the array contains an element. The first One is that it will return -1 and the position of the element to indicate whether it is included. There is no problem in terms of positioning, but it is not semantic enough. Another problem is that it cannot determine whether there are NaN elements.

For example:

const arr1 = ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', NaN]
console.log('%s', arr1.indexOf(NaN))

Result:

-1

ES6 provides the Array.includes() function to determine whether a certain element is included. In addition to being unable to locate, it solves the above problem of indexOf. Two questions. It directly returns true or false to indicate whether it contains an element, and it is also effective for NaN.

const arr1 = ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', NaN]
console.log('%s', arr1.includes('c'))
console.log('%s', arr1.includes('z'))
console.log('%s', arr1.includes(NaN))

Result:

true
false
true

The second parameter of the includes() function indicates the starting position of the judgment.

const arr1 = ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', NaN]
console.log('%s', arr1.includes('d', 1))
console.log('%s', arr1.includes('d', 3))
console.log('%s', arr1.includes('d', 4))

Result:

true
true
false

The second parameter can also be a negative number, indicating the number from the right, but does not change the direction of the search, the search direction is still from left to right.

console.log('%s', arr1.includes('k', -1))
console.log('%s', arr1.includes('k', -2))
console.log('%s', arr1.includes('i', -3))

Result:

false
true
false

Summary:

includes() method, used to detect whether the array contains a certain value, can determine NaN, and directly return true/false , more intuitive; the

indexOf() method is used to find the position of an element. NaN cannot be judged and returns -1, which means it is not included. If not -1, it means the currently included position.

Both methods have their own pros and cons, depending on the actual situation. If the element contains NaN, use includes(), otherwise either can be used.

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

The above is the detailed content of How to determine whether an array contains a certain value 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