ES6 Array.from () cannot get an array element that is undefined or an empty array

Source: Internet
Author: User

This address:http://www.cnblogs.com/veinyin/p/7944072.html

Correct format

1 Let json = {  2     ' 0 ': ' waaaa~ ',  3     ' 1 ': ' Hello, ',  4
         ' 2 ': ' world! ' , 5     Length:3 6}  7  8 Let arr = array.from (JSON); 9 Ten Console.log (arr);

Effect of conversion

The conversion must be a JSON array format, if there is no length in the JSON, then the converted array is an empty array

Comment out the result of line 5th above

Since the Json key is converted to the subscript of the array, you have to be aware of this , and here are a few things that could make the value of the array undefined

1 key cannot be turned into subscript

1 Let json = {  2     ' a ': ' waaaa~ ',  3     ' B ': ' Hello, ',  4
         ' C ': ' world! ' , 5     Length:3 6}  7  8 Let arr = array.from (JSON); 9 Ten Console.log (arr);

2 key greater than or equal to length causes overflow

1 Let json = {  2     ' 0 ': ' waaaa~ ',  3     ' 1 ': ' Hello, ',  4
         ' 3 ': ' world! ' , 5     Length:3 6}  7  8 Let arr = array.from (JSON); 9 Ten Console.log (arr);

Because Arr[2] did not give the value, so gave the undefined, even give arr[3] the value is world! , but I can only get the array to arr[2], so I can't see world! This item

3 is not defined as above.

1 Let json = {  2     ' 0 ': ' waaaa~ ',  3     ' 1 ': ' Hello, ',  4
         ' 3 ': ' world! ' , 5     Length:5 6}  7  8 Let arr = array.from (JSON); 9 Ten Console.log (arr);

This time the length of the big point, you can see if not given is undefined

Ah ah ah ah, the feeling is not clear, anyway, is equivalent to the assignment, the preceding key is the index value, followed by the value of the corresponding array element, length is the size of the array, that's it.

End~~~≥ω≤

ES6 Array.from () cannot get an array element that is undefined or an empty array

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.