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

178
Vistas
Array.from() on IterableIterator seemingly mutates parameter

I'm a bit baffled on this one. My code looks like the following:

let matches = code.matchAll(/regex pattern/g);
if (!Array.from(matches).length) {
    matches = code.matchAll(/regex alt pattern/g);
    if (!Array.from(matches).length) return;
}

console.log(Array.from(matches));

My goal here is to check for a pattern, and if nothing is found, check for an alternate. If still nothing is found, I want to return, else, I'm going to do some work with it.

My issue here is that Array.from(matches) for some reason consumes matches. Let's say that the first regex pattern returns a result. The if-statement will be correctly be skipped over, but the console log will print out an empty array. Nothing in matches. Same goes for the alternative regex. If I check for that early return, matches will be empty again. If I comment it out, however, then that console log has a non-empty array.

According to MDN, Array.from() should be making a shallow copy. Why is it instead wiping away that reference?

about 4 years ago · Juan Pablo Isaza
1 Respuestas
Responde la pregunta

0

If you read the MDN doc on what .matchAll() returns, it is a non-restartable iterator. So, you are correct that Array.from() will consume it and you apparently can't use that iterator again.

So, do the Array.from(matches) once and save it to a variable so you aren't trying to use the iterable more than once. Here's one way to do that:

let matches = Array.from(code.matchAll(/regex pattern/g));
if (!matches.length) {
    matches = Array.from(code.matchAll(/regex alt pattern/g));
    if (!matches.length) return;
}

console.log(matches);
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