In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-09-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/01 Report--
Editor to share with you what is the use of the Array.from () method in ES6. I hope you will get something after reading this article. Let's discuss it together.
Array.from ()
The Array.from method is used to turn two types of objects into real arrays: objects of class arrays (array-like object) and objects that can be traversed (iterable) (including ES6's new data structures Set and Map).
Let arrayLike = {'0forth:' asides, '1clients:' baked, '2percent:' cased, length: 3}; / / ES5 is written as var arr1 = [] .slice.call (arrayLike); / / ['averse,' baked,'c'] / / ES6 is written as let arr2 = Array.from (arrayLike) / / NodeList object let ps = document.querySelectorAll ('p'); Array.from (ps) .forEach (function (p) {console.log (p);}); / / arguments object function foo () {var args = Array.from (arguments) / /...} / / A string is converted into a character array str.split ('') Array.from ('hello') / / [undefined, undefined, undefined] let namesSet = new Set ([' averse,'b']) Array.from (namesSet) / / ['averse,' b'] Array.from ({length: 3}); / / [undefined, undefined, undefined]
For browsers that have not yet deployed this method, you can use the Array.prototype.slice method instead:
Const toArray = () = > Array.from? Array.from: obj = > [] .slice.call (obj) ()
Array.from can also accept the second parameter, which acts like the array's map method to process each element and put the processed value into the returned array.
Array.from (arrayLike, x = > x * x); / equivalent to Array.from (arrayLike) .map (x = > x * x); Array.from ([1je 2jue 3], (x) = > x * x) / / [1,4,9] / / Array.from callback function var arr1 = Array.from ([1m 2m 3], function (item) {return item*item;}) Var arr2 = Array.from ([1jue 2jue 3]) .map (function (item) {return item*item;}); var arr3 = Array.from ([1JI 2je 3], (item) = > item*item); console.log (arr1); / / [1,4,9] console.log (arr2); / / [1,4,9] console.log (arr3); / / [1,4,9]
It is worth reminding that the extension operator (...) can also convert some data structures to arrays.
/ / arguments object function foo () {var args = [... arguments];} / NodeList object [. Document.querySelectorAll ('div')] after reading this article, I believe you have a certain understanding of "what is the use of the Array.from () method in ES6". If you want to know more about it, welcome to 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.
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.