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

174
Views
Recursively combine and concatenate arrays in Javascript

I have two arrays that I would like to map/recurse through and combine all the values into one array.

var array1 = ['steve', 'mike'];
var array2 = ['2020', '2019', '2018'];

The goal is to create an array of all the possible combinations:

['steve2020', 'steve2019', 'steve2018', 'mike2020', 'mike2019', 'mike2018']

I'm not sure if a map for for/next is appropriate or more efficient, but it's hurting my brain right now trying to decipher the recursion logic here. :) Any ideas are much appreciated.

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

You could use a combination of flatMap and map to go over both arrays, combine them and flatten the outcome array.

var array1 = ['steve', 'mike'];
var array2 = ['2020', '2019', '2018'];

const result = array1.flatMap((v) => array2.map((y) => `${v}${y}`));

console.log(result);

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!