JavaScript combine original object with sub array of objects

26
May 15, 2019, at 7:40 PM

I have an large array of objects with some keys:values, one of the keys has a value that is an array of objects. I want to reduce the subarray into a new array of objects.

I can't figure out a solution using mapping thus far.

const warehouse = [
    {
        Server: 'EU',
        Department: 'Paper',
        Suppliers: [
            {
                Name: 'EU Paper',
                Contract: 'Active'
            },
            {
                Name: 'Local Tree',
                Contract: 'Ended'
            }
        ]
    },
    {
        Server: 'US',
        Department: 'Steel',
        Suppliers: [
            {
                Name: 'Steel Research',
                Contract: 'Active'
            },
            {
                Name: 'Heat Vantage',
                Contract: 'Active'
            }
        ]
    }
]

Output should be

const suppliers = [
    {
        Server: 'EU',
        Department: 'Paper',
        Name: 'EU Paper',
        Contract: 'Active'
    },
    {
        Server: 'EU',
        Department: 'Paper',
        Name: 'Local Tree',
        Contract: 'Ended'
    },
    {
        Server: 'US',
        Department: 'Steel',
        Name: 'Steel Research',
        Contract: 'Active'
    },
    {
        Server: 'US',
        Department: 'Steel',
        Name: 'Heat Vantage',
        Contract: 'Active'
    },
]

I can do this with basic JavaScript but i would like to see an option that optimizes for performance

Answer 1

You can use flatMap to loop thru the array and flat the result. Use map to loop thru the Suppliers array.

const warehouse = [{"Server":"EU","Department":"Paper","Suppliers":[{"Name":"EU Paper","Contract":"Active"},{"Name":"Local Tree","Contract":"Ended"}]},{"Server":"US","Department":"Steel","Suppliers":[{"Name":"Steel Research","Contract":"Active"},{"Name":"Heat Vantage","Contract":"Active"}]}]; 
 
let result = warehouse.flatMap(({Suppliers,...r}) => Suppliers.map(o => ({ ...o,...r}))); 
console.log(result);

You can also use concat and map

const warehouse = [{"Server":"EU","Department":"Paper","Suppliers":[{"Name":"EU Paper","Contract":"Active"},{"Name":"Local Tree","Contract":"Ended"}]},{"Server":"US","Department":"Steel","Suppliers":[{"Name":"Steel Research","Contract":"Active"},{"Name":"Heat Vantage","Contract":"Active"}]}]; 
 
let result = [].concat(...warehouse.map(({Suppliers,...r}) => Suppliers.map(o => ({ ...o,...r})))); 
console.log(result);

READ ALSO
npm unable to find correct version of package within Docker

npm unable to find correct version of package within Docker

I am attempting to perform npm install within a docker imageAs part of the package

19
Redis Stream with Node XADD

Redis Stream with Node XADD

How can one use Redis stream in redis-node

38
How resolve Operations and Map with GraphQL-js?

How resolve Operations and Map with GraphQL-js?

basically im trying upload a file with GraphQl js vanilla, but i dont know how resolve operations and map

33