In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-03-31 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/01 Report--
Editor to share with you what the array space in ES6 is, I believe most people do not know much about it, so share this article for your reference, I hope you can learn a lot after reading this article, let's go to know it!
The vacancy of an array
The vacancy of an array means that there is no value at a certain position in the array. For example, the array returned by the Array constructor is empty.
Note that the vacancy is not undefined, and the value of a position is equal to undefined, which is still valuable. Spaces have no value, as can be illustrated by the in operator.
0 in [undefined, undefined, undefined] / / true0 in [,] / / false
The above code shows that the 0 position of the first array has a value, and the 0 position of the second array has no value.
ES5's handling of vacancies is already very inconsistent, and vacancies are ignored in most cases.
ForEach (), filter (), every (), and some () all skip spaces.
Map () skips spaces, but retains this value
Join () and toString () treat vacancies as undefined, while undefined and null are treated as empty strings.
/ / forEach method [,'a'] .forEach ((xmemi) = > console.log (I)) / / filter / every method [,'a'] / / false// map method [,'a'] .map (x = > 1) / / join method [,'a'] / join method [,'a'] / false// map method [,'a'] .map (x = > 1) / [, 1] / join method [,'a'] Null] .join ('#') / / "# adept #" / / toString method [, 'axiomagenical undefined _ null] .toString () / / ", acentine,"
On the other hand, ES6 explicitly converts vacancies into undefined.
/ / the Array.from method converts the empty space of the array to undefined, that is, this method does not ignore the empty space. The Array.from (['a', undefined, "b"] / / extension operator (...) also converts spaces to undefined. [...] / / ["a", undefined, "b"] / / copyWithin () will be copied with vacant space. [,] .copyWithin (2 fill 0) / / [, "a", "a"] / copyWithin () treats spaces as normal array positions. The new Array (3) .fill ('a') / / ["a", "a", "a"] / / for...of loop also traverses empty spaces. Let arr = [,]; for (let i of arr) {console.log (1);} / 1 / 1 / / in the above code, the array arr has two empty spaces, and for...of does not ignore them. If the traversal is changed to the map method, the empty space will be skipped. / / entries (), keys (), values (), find (), and findIndex () process vacancies as undefined. / / entries () [. [,'a'] .values ()] / / [[0 undefined undefined], [1, "a"]] / / keys () [. [,'a'] .keys ()] / / [0J 1] / / values () [. [,'a'] .values ()] / [undefined, "a"] / / find () [ 'a'] .find (x = > true) / / undefined / / findIndex () [,'a'] .findIndex (x = > true) / / 0 / / because the rules for handling vacancies are very inconsistent Therefore, it is recommended to avoid vacant seats. The above is all the content of the article "what are the empty spaces in the array in ES6". Thank you for reading! I believe we all have a certain understanding, hope to share the content to help you, if you want to learn more knowledge, welcome to follow the industry information channel!
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.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.