Passing Array.from as a callback to Array.flatMap causes an error: "TypeError: 0 is not a function"
const x = ["12"].flatMap(Array.from)
console.log(x)
Despite this Array.from is usable as a function normally:
const f = Array.from
console.log(f("12"))
I have found a way around this:
const x = ["12"].flatMap(e => Array.from(e))
console.log(x)
I would like someone to tell me:
Array.from
accepts up to 3 parameters, the second one being map function while .flatMap
will pass to it iteration index as the second parameter.
So, on the first iteration, Array.from
will recieve 0
as a second parameter, which, indeed, is not a function that Array.from
expects.
To demonstrate it more clearly, here is a snippet. This line:
["12"].flatMap(Array.from);
is functionally equivalent to:
Array.from("12", 0, ["12"]);
which is an incorrect signature for Array.from
.