Lets say I have an array with values
product: [{
'product_id': 247
'order_id': 4
}
{
'product_id': 245
'order_id': 3
}
{
'product_id': 247
'order_id': 3
}]
I have button passing 'product_id' and 'order_id', and every time the button is pressed, I want an object to be something like this, the 'product_id' gets added at the end of the object array accordingly.
[{4: [247, 247, 247], 3: [247, 245, 247]}]
I have tried using different array methods
var productarray = product
productarray.reduce((order, obj)=> {
let key = obj['order_id']
if (!order[key]) {
order[key] = []
}
order[key].push(obj.product_id);
})
but I'm not able to get the result I'm looking for. Sorry if this a vague question. Thank you in advance.