Monday, October 7, 2024
Google search engine
HomeLanguagesJavascriptHow to create an array of objects from multiple arrays in JavaScript...

How to create an array of objects from multiple arrays in JavaScript ?

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:

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'}

Whether you’re preparing for your first job interview or aiming to upskill in this ever-evolving tech landscape, neveropen Courses are your key to success. We provide top-quality content at affordable prices, all geared towards accelerating your growth in a time-bound manner. Join the millions we’ve already empowered, and we’re here to do the same for you. Don’t miss out – check it out now!

Dominic Rubhabha-Wardslaus
Dominic Rubhabha-Wardslaushttp://wardslaus.com
infosec,malicious & dos attacks generator, boot rom exploit philanthropist , wild hacker , game developer,
RELATED ARTICLES

Most Popular

Recent Comments