Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

468
Vistas
Why do I get TypeError: 0 is not a function when passing Array.from as a callback to Array.flatMap?

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:

  1. Why do I get an error?
  2. Why do I get such an astonishingly unhelpful error message?
about 4 years ago · Juan Pablo Isaza
2 Respuestas
Responde la pregunta

0

It happends because the flatMap passes more than 1 argument to the callback. The second argument is the index of the element. Array.from function expects a replacer function in the second argument. So this shortened version passed an index in place of a function.

What you're doing is equivalent to calling:

Array.from("12", 0)

Instead of a proper replacer like:

console.log(Array.from("12", x => x + x));

about 4 years ago · Juan Pablo Isaza Denunciar

0

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.

about 4 years ago · Juan Pablo Isaza Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda