Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

189
Visualizações
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 Respostas
Responde à pergunta

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 Relatório

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 Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda