Home > Web Front-end > JS Tutorial > body text

How to use the Array.includes() function in ES6

小云云
Release: 2018-02-05 10:28:20
Original
1903 people have browsed it

This article mainly introduces the usage of the Array.includes() function in ES6. Friends who need it can refer to it. I hope it can help everyone.

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 parameters when determining whether the array contains an element. Disadvantages, the first 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.


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

ES6 provides the Array.includes() function to determine whether a certain element is included, except that it cannot In addition to positioning, it solves the above two problems of indexOf. 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

The second of the includes() function The parameter 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

The second parameter can also be a negative number, indicating from Count from the right, but do 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

Related recommendations:

Object.assign() method in ES6

##ES6 Promise extended always method instance detailed explanation

ES6 block-level scope detailed explanation

The above is the detailed content of How to use the Array.includes() function 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
Popular Tutorials
More>
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!