Array.from 1

Array.from

console.log(Array.from('foo'));
// expected output: Array ["f", "o", "o"]

console.log(Array.from([1, 2, 3], x => x + x));
// expected output: Array [2, 4, 6]

Here is what the above code is Doing:
1. Array.from() creates a new, shallow-copied Array instance from an array-like or iterable object.
2. Array.from() has an optional parameter mapFn, which allows you to execute a map function on each element of the array (or subclass object) that is being created. More clearly, Array.from(obj, mapFn, thisArg) has the same result as Array.from(obj).map(mapFn, thisArg), except that it does not create an intermediate array. This is especially important for certain array subclasses, like typed arrays, since the intermediate array would necessarily have values truncated to fit into the appropriate type.
3. Array.from() has an optional parameter thisArg, which serves as the this value for each invocation of the mapFn.

Similar Posts