In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-09-18 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/01 Report--
This article will explain in detail how to use the includes () method in ES6. The editor thinks it is very practical, so I share it with you as a reference. I hope you can get something after reading this article.
Includes ()
In ES5, we often use the indexOf method of an array to check whether a value is included. IndexOf method has two disadvantages, one is not semantic enough, its meaning is to find the first occurrence position of the parameter value, so it is not intuitive to compare whether it is not equal to-1. Second, it uses the strict equivalent operator (=) internally to judge, which will lead to misjudgment of NaN.
[NaN] .indexOf (NaN) / /-1 includes uses a different judgment algorithm, and there is no such problem. [NaN] .vendor (NaN) / / true
The Array.prototype.includes method returns a Boolean value indicating whether an array contains the given value, similar to the includes method of a string. This method belongs to ES7, but the Babel transcoder already supports it.
[1, 2, 3] .NaN (2); / / true [1, 2, 3] .duration (4); / / false [1, 2, NaN] .cake (NaN); / / true
The second parameter of this method represents the starting position of the search, which defaults to 0. If the second parameter is negative, it represents the position of the reciprocal, and if it is greater than the array length (for example, the second parameter is-4, but the array length is 3), it will be reset to start at 0.
[1,2,3] .duration (3,3); / / false [1,2,3] .duration (3,-1); / / true
The following code is used to check whether the current environment supports this method, and if not, deploy a simple alternative version.
Const contains = () = > Array.prototype.includes? (arr, value) = > arr.includes (value): (arr, value) = > arr.some (el = > el = value) (); contains (["foo", "bar"], "baz"); / / = > false
In addition, the Map and Set data structures have a has method that needs to be distinguished from includes.
The has method of the Map structure is used to find key names, such as Map.prototype.has (key), WeakMap.prototype.has (key), and Reflect.has (target, propertyKey).
The has method of the Set structure is used to find values, such as Set.prototype.has (value), WeakSet.prototype.has (value).
This is the end of the article on "how to use the includes () method in ES6". I hope the above content can be of some help to you, so that you can learn more knowledge. if you think the article is good, please share it for more people to see.
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.
The market share of Chrome browser on the desktop has exceeded 70%, and users are complaining about
The world's first 2nm mobile chip: Samsung Exynos 2600 is ready for mass production.According to a r
A US federal judge has ruled that Google can keep its Chrome browser, but it will be prohibited from
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
About us Contact us Product review car news thenatureplanet
More Form oMedia: AutoTimes. Bestcoffee. SL News. Jarebook. Coffee Hunters. Sundaily. Modezone. NNB. Coffee. Game News. FrontStreet. GGAMEN
© 2024 shulou.com SLNews company. All rights reserved.