Array Syntax series: Array.from ()

Source: Internet
Author: User

Array.from()You can create an array object in the following ways:

    • Pseudo-Array object ( length any object that has a property and several indexed properties)
    • Can iterate over objects (can get elements in objects, such as map and Set, etc.)

Array.from()Method has an optional parameter mapFn that allows you to execute the method again on the last generated array map before returning. Which means  Array.from(obj, mapFn, thisArg)  it's equivalent Array.from(obj).map(mapFn, thisArg);

example Array from a Stringarray.from (' Foo '); //["F", "O", "O"] Array from a Setlet S=NewSet ([' foo ', window]); Array.from (s); //["foo", Window] Array from a Maplet M=NewMap ([[1, 2], [2, 4], [4, 8]]); Array.from (m); //[ [1, 2], [2, 4], [4, 8]]Array from an Array-like object (arguments)functionf () {returnarray.from (arguments);} F (1, 2, 3);//[1, 2, 3]Using arrow functions and Array.from//Using a arrow function as the map function to//manipulate the ElementsArray.from ([1, 2, 3], x = x +x); //[2, 4, 6]//Generate A sequence of numbers//Since The array is initialized with ' undefined ' on each position,//The value of ' V ' below would be ' undefined 'Array.from ({length:5}, (V, i) = =i);//[0, 1, 2, 3, 4]

Array Syntax series: Array.from ()

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.