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

204
Views
Merge each value in an array to each value in another array - Javascript

I have two arrays

let arr1 = [{a: 1, b: 2}, {a: 3, b: 4}]
let arr2 = [{c: 5}, {c: 6}]

I am trying to create a new array such that each value in arr1 is mapped to each value in arr2. Something like this,

[1-2-5, 1-2-6, 3-4-5, 3-4,6]

I know that this can be done using two for loops. One to loop on arr1 and a nested for loop that loops over arr2. But I am looking for a cheap and efficient method to do it. Any help would be much appreciated! Thanks in advance. Cheers!

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

0

You could use map() to achieve this.

let arr1 = [{a: 1, b: 2}, {a: 3, b: 4}]
let arr2 = [{c: 5}, {c: 6}]

const result = arr1.flatMap((x) => {
  return arr2.map((y) => `${x.a}-${x.b}-${y.c}`);
});

console.log(result);

Because you can't use - in a number, storing the values in separate arrays would be an option.

let arr1 = [{a: 1, b: 2}, {a: 3, b: 4}]
let arr2 = [{c: 5}, {c: 6}]

const result = arr1.flatMap((x) => {
  return arr2.map((y) => [x.a, x.b, y.c]);
});

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!