In this article, we will try to understand how we may create an array of objects from multiple arrays in JavaScript. There are some common methods:
- console.log() method
- Array.map() method
- for..loop method
Let us first try to analyze how we may create an array of objects using the below-enlightened syntax.
Syntax: We use the following syntax in order to create as well as analyze an array of multiple objects:
let array_of_objects = [ { property_name : property_value, ... }, { property_name : property_value, ... }, ... ]
Let us have a quick look over the below-shown example below that will help us to clarify the above syntax.
Example 1: In this example, we will simply create an array of objects, and then we are going to output it using the console.log() method.
Javascript
let books_details = [ { book_id: 1, book_name: "Live Life Happily..!!" , book_author: "Aman" , book_copies: 10, }, { book_id: 2, book_name: "Be Energetic Always..!!" , book_author: "Ram" , book_copies: 20, }, { book_id: 3, book_name: "Earn Respect..!!" , book_author: "Shyam" , book_copies: 30, }, ]; console.log(books_details); |
Output:
[ { book_id: 1, book_name: 'Live Life Happily..!!', book_author: 'Aman', book_copies: 10 }, { book_id: 2, book_name: 'Be Energetic Always..!!', book_author: 'Ram', book_copies: 20 }, { book_id: 3, book_name: 'Earn Respect..!!', book_author: 'Shyam', book_copies: 30 } ]
Let us see the only single approach by which we could easily solve our problem statement which is how to create an array of objects from multiple arrays, using the above-shown example.
Example 2: In this example, we will create multiple arrays (one after another), and just after that, we will use Array.map() method to traverse our array and then while returning an object, we will embed all the properties and their corresponding values inside it which is available in the rest of the arrays themselves.
Javascript
let book_ids = [1, 2, 3]; let book_names = [ "Live Life Happily..!!" , "Be Energetic Always..!!" , "Earn Respect..!!" , ]; let book_authors = [ "Aman" , "Ram" , "Shyam" ]; let book_copies = [10, 20, 30]; let books_details = book_ids.map((id, index_value) => { return { id: id, book_name: book_names[index_value], book_author: book_authors[index_value], book_copies: book_copies[index_value], }; }); console.log(books_details); |
Output:
[ { id: 1, book_name: 'Live Life Happily..!!', book_author: 'Aman', book_copies: 10 }, { id: 2, book_name: 'Be Energetic Always..!!', book_author: 'Ram', book_copies: 20 }, { id: 3, book_name: 'Earn Respect..!!', book_author: 'Shyam', book_copies: 30 } ]
Example 3: Using for-loop: By using for_loop we are creating an array object from multiple arrays.
Javascript
const no = [1, 2, 3]; const player = [ "Virat kohli" , "Ab de villiers" , "Glenn Maxwell" ]; const country = [ "India" , "South africa" , "Australia" ]; for (let i = 0; i < no.length; i++) { const newArray = { no: no[i], Player_name: player[i], country_name: country[i], } console.log(newArray); }; |
Output:
{no: 1, Player_name: 'Virat kohli', country_name: 'India'} {no: 2, Player_name: 'Ab de villiers', country_name: 'South africa'} {no: 3, Player_name: 'Glenn Maxwell', country_name: 'Australia'}