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

169
Views
JS create an array with objects from other array
let array1 = [1,2,3,4,5]
let array2 = [6,7,8,9,10]
let array3 = [ {x: array1, y: array2} ]

How to write correctly loop, so in output I will get 1 array with 5 ojects inside?

//Expected output

[ {x: 1, y: 6}, {x: 2, y: 7}, {x: 3, y: 8}, {x: 4, y: 9}, {x: 5, y: 10}]
about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

You should loop through all the values of the first array with .map which generates a new array. The function inside of .map takes the value from array1 as value and array2 as array2[index];

let array1 = [1,2,3,4,5]
let array2 = [6,7,8,9,10]
let array3 = array1.map(function (value, index) {
    return {x: value, y: array2[index]}
})
console.log(array3);

I've intentionally used slightly more verbose solution that could be required for easier readability and understanding.

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!