Identifies the element type of the array-like or iterable.
Identifies returned type of the array
An array-like object or iterable to convert to an array.
OptionalmapFn: ArrFromMapFn<T, U>
A mapping function to call on every element of the array. If provided, every
value to be added to the array is first passed through this map function, and the return
value is added to the array instead. The function is called with the value and its index (mapFn(value, index))
and the thisArg as this.
OptionalthisArg: any
Value of 'this' used to invoke the mapfn.
polyArrFrom("Hello");
// [ "H", "e", "l", "l", "o" ]
polyArrFrom(new Set(["Hello", "Darkness", "my", "old", "friend"]));
// ["Hello", "Darkness", "my", "old", "friend"]
let map = new Map([
[ 1, "Hello" ],
[ 2, "Darkness" ],
[ 3, "my" ],
[ 4, "old" ],
[ 5, "friend"]
]);
polyArrFrom(map.values());
// ["Hello", "Darkness", "my", "old", "friend"]
polyArrFrom(map.keys());
// [ 1, 2, 3, 4, 5 ]
polyArrFrom(map.entries());
// [ [ 1, "Hello" ], [ 2, "Darkness" ], [ 3, "my" ], [ 4, "old" ], [ 5, "friend"] ]
polyArrFrom(map, ([ key, value ]) => ({ [key]: value }));
// [ {"1": "Hello"}, {"2": "Darkness"}, {"3": "my"}, {"4": "old"}, {"5": "friend"} ]
The polyArrFrom creates an new shallow-copied array from an array-like object or an iterable.
Like the native
Array.from():[Symbol.iterator], which is only read once) is iterated using the iterator it returns, so an iterator which is not iterable (eg. one created by createIterator or createRangeIterator) returns an empty array (use iterToArray to read its values). Otherwise (when the iterator function isnullorundefined) the value is used as an array-like value (eg.argumentsor aNodeList), so every index up to its length is read (an empty slot or missing index isundefined) and any other value (eg. a number) returns an empty array. An array is always read using its indexes, like the native array iterator its length is read for each index, so a value added to it while it is read (eg. by themapFn) is also read and the reading stops at its new length when values are removed, while (like the native function) any other array-like is read up to the length it had when it was called.Stringobject) is split into its code points (so a surrogate pair is a single value), this is also done for runtimes which don't supportSymbol.iterator.Mapreturns its[key, value]entries and aSetits values, this is also done (since 0.20.0) for aMaporSetwhich has no iterator function (eg. on IE 11) using itsforEach().mapFnis called with the value and its index (without using its owncallproperty), and each value is added as an own property of the new array (even whenArray.prototypehas a setter or read-only property for the index).nullorundefinedvalue, amapFnwhich is not a function, an iterator function which is not a function or one which doesn't return an iterator, or an iterator result which is not an object throws aTypeError, and a length which is longer than an array can be (2^32 - 1) throws aRangeError.