Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Make an array of array from array of object

I just want to make an array from my data and list down each material base on the status.
My array show below..

let data = [
        {
            Name: 'House 1', 
            Details:[
                {Materials: 'Door', Status: 'Delivered'},
                {Materials: 'Closet', Status: 'Pending'},
                {Materials: 'Chair', Status: 'Pending'},
                {Materials: 'Kitchen', Status: 'Delivered'},
                {Materials: 'Door Knob', Status: 'Delivered'}
            ]
        },

        {
            Name: 'House 2', 
            Details:[
                {Materials: 'Cabinet', Status: 'Delivered'},
                {Materials: 'Kitchen', Status: 'Delivered'},
                {Materials: 'Door', Status: 'Ongoing'},
                {Materials: 'Chair', Status: 'Ongoing'},
                {Materials: 'Window', Status: 'Cancelled'}
            ]
        },
        //Other codes here...
    ];

I want to list down the status for each material.

Expected Output:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

[
    0: 'House 1'
        [
            0: 'Delivered': [ 0: 'Door', 1: 'Kitchen', 2: 'Door Knob' ]
            1: 'Pending': [ 0: 'Closet, 1: 'Chair' ]
        ]
    1: 'House 2'
        [
            0: 'Cancelled': [ 0: 'Window' ]
            1: 'Delivered': [ 0: 'Cabinet', 1: 'Kitchen' ]
            2: 'Ongoing': [ 0: 'Chair', 1: 'Door' ]
        ]
]
//where 0:, 1:, 2: is the index of each array(*nested array)
//i just added index like what you see in console.log(*but it's not required to show in final output)

I’m trying to use forEach and .reduce but it seems my code doesn’t go well.

I hope you can resolve my problem 🙂

>Solution :

The structure you want cannot be represented solely by arrays; use objects instead for key-value pairs. You can use Array#reduce to group elements by their status into a single object.

let data=[{Name:"House 1",Details:[{Materials:"Door",Status:"Delivered"},{Materials:"Closet",Status:"Pending"},{Materials:"Chair",Status:"Pending"},{Materials:"Kitchen",Status:"Delivered"},{Materials:"Door Knob",Status:"Delivered"}]},{Name:"House 2",Details:[{Materials:"Cabinet",Status:"Delivered"},{Materials:"Kitchen",Status:"Delivered"},{Materials:"Door",Status:"Ongoing"},{Materials:"Chair",Status:"Ongoing"},{Materials:"Window",Status:"Cancelled"}]},];
let res = Object.fromEntries(data.map(o => [o.Name, o.Details.reduce((acc, curr) => {
  (acc[curr.Status] ??= []).push(curr.Materials);
  return acc;
}, {})]));
console.log(res);
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading