TIL how JS Array concat determines "arrayness" of arguments
[].concat(1);
// [ 1 ]
[].concat([1, 2, 3]);
// [ 1, 2, 3 ]
[].concat({
__proto__: Array.prototype,
length: 1,
0: "Hi"
});
// [ Array { '0': 'Hi', length: 1 } ]
[].concat({
length: 1,
0: "Hi",
[Symbol.isConcatSpreadable]: true,
});
// [ 'Hi' ]
syntax highlighting by codehost
this differs from Array.from
which will directly turn objects with length
into arrays without checking for any other properties
want a sequence of numbers? just Array.from({length: 10}).map((e, i) => i*2)
:)
this of course differs from Array(10).map((e, i) => i*2)
which never runs the map
lambda unless you first .fill
the array.
yep! 😊
or my favourite, [...Array(10)].map((e, i) => i*2)