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

135
Views
How to combine 2 arrays in a way that 1st element of array1 and 1st element of array2 and so on form a new array

I have 2 arrays. The first contains the years like [2021,2020,2019,2018,2017] and the second contains number of occurrence like [2,3,1,3,3] I want the new array to look like this [[2021,2],[2020,3],[2019,1],[2018,3],[2017,3]] How to do this? Please help! Thank you in advance.

UPDATE:

const merge = (y, o) => {
    const res = [];
    y.forEach((year, i) =>
              res.push(`${year},${o[i]}`)
             );
    return res;
  };
  console.log(merge(allyears,farray))

I tried doing this and it works fine but the new array looks like this

["2021,3","2020,1","2019,2","2018,3","2017,3"]

How to make it look this way

[[2021,2],[2020,3],[2019,1],[2018,3],[2017,3]]
about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

Make sure that the length of 2 arrays is equal

So we have:

const arr1 = [2021,2020,2019,2018,2017];
const arr2 = [2,3,1,3,3];

const combine = arr1.map((value, index) => ([value, arr2[index]]))

console.log(combine)

P/s: There are some other solutions that you can use, but only that one above

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!