Alejandro Perez Alejandro Perez - 1 year ago 87
Javascript Question

How to create an array of objects from multiple arrays

Given the following arrays:

var ids = [1,2,3]; //Hundreds of elements here
var names = ["john","doe","foo"]; //Hundreds of elements here
var countries = ["AU","USA,"USA"]; //Hundreds of elements here

What's the best way performance-wise to generate an array of objects with a similar structure to this:

var items = [

Answer Source

You should be able to simply map through all ids, keeping a reference to your index, and build your object based on that index.

var items =, index) => {
  return {
    id: id,
    name: names[index],
    country: countries[index]
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download