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

青灯夜游
Release: 2022-03-07 18:35:31
Original
10411 people have browsed it

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))
Copy after login

Result:

-1
Copy after login

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))
Copy after login

Result:

true false true
Copy after login

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))
Copy after login

Result:

true true false
Copy after login

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))
Copy after login

Result:

false true false
Copy after login

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!

Related labels:
source:php.cn
Statement of this Website
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
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!