Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

188
Views
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 answers
Answer question

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 Report

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 Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!