In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/01 Report--
This article focuses on "how to convert objects into arrays by es6". Interested friends may wish to take a look. The method introduced in this paper is simple, fast and practical. Let's let the editor take you to learn "how es6 converts objects into arrays".
In es6, you can use the from () method of the Array object to convert an object into an array, which converts a class array object or traverable object into a real array; the syntax "Array.from".
The operating environment of this tutorial: windows7 system, ECMAScript version 6, Dell G3 computer.
In es6, you can use the from () method of the Array object to turn objects into arrays.
The Array.from () method converts a class array object or traverable object into a real array.
So what is a class array object? The so-called class array object, the most basic requirement is the object with length attribute.
1. Convert a class array object to a real array:
Let arrayLike = {0: 'tom', 1:' 65 male, 2: 'male', 3: ['jane','john','Mary'],' length': 4} let arr = Array.from (arrayLike) console.log (arr) / / ['tom','65',' male', ['jane','john','Mary']]
So, what if you remove the length attribute from the above code? It turns out that the answer will be an empty array of length 0.
Here, change the code to have the length attribute, but the property name of the object is no longer of numeric type, but of other string types. The code is as follows:
Let arrayLike = {'name':' tom', 'age':' 65th, 'sex':' male', 'friends': [' jane','john','Mary'], length: 4} let arr = Array.from (arrayLike) console.log (arr) / / [undefined, undefined]
You will find that the result is an array of length 4 and elements of undefined.
Thus, to convert a class array object into a real array, you must have the following conditions:
This type of array object must have a length property that specifies the length of the array. If there is no length attribute, the converted array is an empty array.
The attribute name of this type of array object must be a numeric or string number.
Ps: the attribute names of this type of array objects can be in quotation marks or not.
2. Convert the data of Set structure into a real array:
Let arr = [12Person45597 let arr] let set = new Set (arr) console.log (Array.from (set)) / / [12Perry 4597pence97pence9797564 Meijue 13445642]
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. As follows:
Let arr = [12797, 564] let set = new Set (arr) console.log (Array.from (set, item = > item + 1)) / / [13,46,98, 9798, 565,135, 45643]
3. Convert a string to an array
Let str = 'hello world leaders console.log (Array.from (str)) / / ["h", "e", "l", "o", "w", "o", "r", "l", "d", "!"]
4. The Array.from parameter is a real array:
Console.log (Array.from ([12.45pr. 47pr. 56pr. 213pr 4654154])
In a case like this, Array.from returns an identical new array
At this point, I believe you have a deeper understanding of "how es6 converts objects into arrays". You might as well do it in practice. Here is the website, more related content can enter the relevant channels to inquire, follow us, continue to learn!
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.