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

169
Vistas
How can I extract letters of an array of strings into an array, split by space in JavaScript?

Here is a code snippet that explains what I am trying to do..

const names = ["foo", "bar", "baz"];
let actual = [];
// Implementation here..
const expected = ['f', 'o', 'o', ' ', 'b', 'a', 'r', ' ', 'b', 'a', 'z'];

// Tests
if (actual.length !== expected.length) {
  console.error("Fail - Arrays not in equal length.");
}
for (let i = 0; i < expected.length; i++) {
  if (actual[i] !== expected[i]) {
    console.error('Fail - Letters are not equal in indices.');
    break;
  }
}

I do have an implementation for it but is there a simpler/elegant way to do this instead of using nested loops and an if condition to check to see if we are iterating over the last element?

Here what works for me which I do not like and want to improve:

for (const name of names) {
  for (const letter of [...name]) {
    actual.push(letter);
  }
  if (names[names.length - 1] !== name) {
    actual.push(' ');
  }
}
about 4 years ago · Juan Pablo Isaza
1 Respuestas
Responde la pregunta

0

Sounds like it'd be easy to just join the array of strings by a space, then turn that into an array:

const names = ["foo", "bar", "baz"];
const result = [...names.join(' ')];

console.log(result);

(you might be tempted to .split('') for more elegant looking chaining, but that has problems with certain characters)

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