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

194
Vistas
Putting two array strings together
const template = {
      example: {
          simple: ['Hey', 'Origami'],
          extra: ['Its me', 'Dotcom']
      }
}

I want to join each element of template.example.simple & template.example.extra together.

So the results would be:

['Hey', 'Origami', 'Hey Its me', 'Hey Dotcom', 'Origami Its me', 'Origami Dotcom']

In order to accomplish that, what I'm doing right now is:

const template = {
      example: {
          simple: ['Hey', 'Origami'],
          extra: ['Its me', 'Dotcom']
      }
}
const example = template.example.simple;

template.example.simple.forEach((s) => {
     let extra = s;
     template.example.extra.forEach((a) => {
         extra += ` ${a}`;
         example.push(extra);
         extra = s;
     });
});

console.log(example);
//example = ['Hey', 'Origami', 'Hey Its me', 'Hey Dotcom', 'Origami Its me', 'Origami Dotcom']

So my question is if there's a simpler method of accomplishing this, and how would you improve this?

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

0

You could use Array.prototype.concat() to merge two lists into one.

On your code, it should look something similar to:

const newValues = template.example.simple.concat(template.example.extra);

Here you can find the documentation and different usage examples.

about 4 years ago · Juan Pablo Isaza Denunciar

0

Simpler is a very relative term. I would not call this method simpler/easier to read, but for the sake of giving alternatives, this one liner should do the trick. It uses map to select both the original value from simple and an extra map to get simple and extra together (and the spread operator ... to flatten them)

const template = {
      example: {
          simple: ['Hey', 'Origami'],
          extra: ['Its me', 'Dotcom']
      }
};

const example = Array.prototype.concat(...template.example.simple.map(p=>[p,...template.example.extra.map(e=>p + ' ' + e)]));

console.log(example);

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