creating multiple objects in array js code example

Example 1: javascript create array of objects from multiple 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

// Create the object array
var items = ids.map((id, index) => {
  return {
    id: id,
    name: names[index],
    country: countries[index]
  }
});

// Result
var items = [
    {id: 1, name: "john", country: "AU"},
    {id: 2, name: "doe", country: "USA"},
    ...
];

Example 2: how to add multiple elements to A new array javascript

let vegetables = ['parsnip', 'potato']
let moreVegs = ['celery', 'beetroot']

// Merge the second array into the first one
// Equivalent to vegetables.push('celery', 'beetroot')
Array.prototype.push.apply(vegetables, moreVegs)

console.log(vegetables)  // ['parsnip', 'potato', 'celery', 'beetroot']