Search
 
SCRIPT & CODE EXAMPLE
 

JAVASCRIPT

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"},
    ...
];
Comment

PREVIOUS NEXT
Code Example
Javascript :: jquery find div with data attribute value 
Javascript :: javascript includes 
Javascript :: how to change text color in react 
Javascript :: javaSript string first words to upper case 
Javascript :: javascript check if variable is a jquery object 
Javascript :: notice before reload js 
Javascript :: jqiery check if scroll to end 
Javascript :: how to delete element in list javascript 
Javascript :: ajax request in javascript 
Javascript :: javascript shuffle string 
Javascript :: how to scroll smoothly in to the top in react js 
Javascript :: Reverse numbers from an array in javascript 
Javascript :: how to know if a number has a decimal number js 
Javascript :: how to sort an array of objects by two fields in javascript 
Javascript :: jquery table each rows with class 
Javascript :: layout nextjs 
Javascript :: js check if string includes from array 
Javascript :: javascript get gps location 
Javascript :: how to get which key is pressed in javascript 
Javascript :: datatables ajax.reload(); 
Javascript :: Javascript form check to see if return or enter was pressed 
Javascript :: how to use static file node js 
Javascript :: Get LocalStorage from WebView react native 
Javascript :: react index.jsx example 
Javascript :: orbit controls drei 
Javascript :: How to access return value of promise 
Javascript :: LF would be replaced by CRLF in package-lock.json 
Javascript :: nestjs vscode debug 
Javascript :: how to customize js alert box 
Javascript :: foreach db mongodb 
ADD CONTENT
Topic
Content
Source link
Name
1+7 =