Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How does es6 determine whether an array contains a value

2025-01-22 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/01 Report--

Editor to share with you how es6 determines whether an array contains a certain value. I hope you will get something after reading this article. Let's discuss it together.

In es6, you can use the includes () method of an array to determine whether an array contains a value, which can be used to detect whether an array contains a value.

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

In ES5,Array, indexOf has been provided to find the location of an element, if it does not exist, it will return-1, but this function has two small shortcomings in judging whether the array contains an element. The first is that it will return-1 and the position of the element to indicate whether it contains it or not. Another problem is that it is not possible to determine whether there is an element of NaN.

For example:

Const arr1 = ['axiao,' baked, 'cased,' dashed, 'eyed,' faded, 'grubbed,' hacked, 'ified,' jacked, 'kink, NaN] console.log ('% slots, arr1.indexOf (NaN))

Results:

-1

ES6 provides an Array.includes () function to determine whether it contains an element, which solves the above two problems of indexOf in addition to being unable to locate. It directly returns true or false to indicate whether it contains elements, which is also valid for NaN.

Const arr1 = ['axiang,' baked, 'cased,' dashed, 'eyed,' faded, 'gashed,' hacked, 'iFeed,' jaded, 'kink, NaN] console.log ('% slots, arr1.includes ('c')) console.log ('% slots, arr1.includes ('z')) console.log ('% slots, arr1.includes (NaN)

Results:

Truefalsetrue

The second argument of the includes () function represents the starting position of the judgment.

Const arr1 = ['axiang,' baked, 'cased,' dashed, 'eyed,' faded, 'galled,' hacked, 'iFeed,' jaded, 'kink, NaN] console.log ('% slots, arr1.includes ('dudes, 1)) console.log ('% slots, arr1.includes ('dudes, 3)) console.log ('% slots, arr1.includes ('dudes, 4))

Results:

Truetruefalse

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

Console.log ('% slots, arr1.includes ('kills,-1)) console.log ('% slots, arr1.includes ('kills,-2)) console.log ('% slots, arr1.includes ('iTunes,-3))

Results:

Falsetruefalse

Summary:

The includes () method, which is used to detect whether the array contains a certain value, can judge NaN and return true/false directly, which is more intuitive.

The indexOf () method, which is used to find the location of an element, cannot judge NaN, and returns-1, that is, it does not contain, and non-1 means the current included location.

Each of the two methods has its own advantages and disadvantages, depending on the actual situation. If the element contains NaN, use includes (), otherwise both will do.

After reading this article, I believe you have a certain understanding of "how es6 determines whether a certain value is included in the array". If you want to know more about it, please follow the industry information channel. Thank you for reading!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report