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 ()