I'm trying to transpose an object, so I can rener them to a table as a single row per product.
data is coming from a MySQL database, I can modify the query so that uses a case for the various products, but it wouldn't be scalable.
Data is coming out like this, which I want to transform/transpose
[
{
label: 'Product 1',
color: 'green',
discontinued: 1,
inventory: 5
},
{
label: 'Product 2',
color: 'green',
discontinued: 1,
inventory: 13
},
{
label: 'Product 2',
color: 'red',
discontinued: 1,
inventory: 1
},
{
label: 'Product 3',
color: 'green',
discontinued: 1,
inventory: 1
},
{
label: 'Product 1',
color: 'blue',
discontinued: 1,
inventory: 0
},
{
label: 'Product 2',
color: 'blue',
discontinued: 1,
inventory: 2
},
{
label: 'Product 3',
color: 'blue',
discontinued: 1,
inventory: 1
},
]
to this:
[
{
label: 'Product 3',
blue: 1,
green: 1,
red: 0,
discontinued: 1,
},
{
label: 'Product 2',
blue: 1,
green: 13,
red: 1,
discontinued: 1,
}, {
label: 'Product 1',
blue: 0,
green: 5,
red: 0,
discontinued: 1,
},
]
I'm looking over these articles but not sure how to convert it so it can use an object rather than an array
Transform array of objects into a transposed array of objects
https://techbrij.com/convert-column-to-row-javascript-array-pivot