I have a large array of objects and I am trying to consolidate to where I have one array of objects that looks like below: Each item(ex:banana) will be in 2 separate objects as I am trying to aggregate all buy orders and sell orders, and these are different prices/data.
I have tried grabbing the unique pairings (banana, apple, orange, etc.) and mapping through that and inside another map of the data, but I can't figure it out.
let uniquePairing = Array.from([...new Set(data.map(item => item["name"]))])
**let data = [{
name:'banana,
price:( average cost)
type:buy
items: ( total bananas)
fee: ( total fees)
},
{
name:'banana,
price:( average cost)
type:sell
items: ( total bananas)
fee: ( total fees)
},
{ apples...
]**
Example of data
let data = [
{
name:"banana",
price:1,
type: "buy",
fee: 0.5,
items:25
},
{
name:"banana",
price:1.2,
type: "buy",
fee: 0.5,
items:25
},
{
name:"banana",
price:2,
type: "sell",
fee: 0.5,
items:25
},
{
name:"apple",
price:1,
type: "buy"
fee: 0.5
items:25
},
{
name:"apple",
price:1.2,
type: "buy",
fee: 0.5,
items:25
},
{
name:"apple",
price:2,
type: "sell",
fee: 0.5,
items:25
}
]
You could do it like this: