In my scenario, I am seeking assistance with merging the values of objects in an array if the id matches the warehouse_item_id. Specifically, there are two objects that need to be merged: id 191 and id 52 because id 52 has a warehouse_item_id of 191. Please refer to the code snippet and JSON response below.
CODE
const yeah = yes.filter(a => a.id === a.warehouse_item_id);
JSON Response
yes = [
{
"id": 191,
"warehouse_id": 24,
"ingredient_id": 65,
"expiration_date": "2019-07-31",
"available_stocks": 7,
"ingredient": {
"id": 65,
"name": "erg",
"SKU": "1000064",
"default_unit": {
"id": 26,
"name": "Milliliter"
},
"purchase_price": 50
}
},
{
"id": 192,
"warehouse_id": 24,
"ingredient_id": 66,
"expiration_date": "2019-09-18",
"available_stocks": 33994,
"ingredient": {
"id": 66,
"name": "gvf",
"SKU": "1000065",
"default_unit": {
"id": 27,
"name": "Gram"
},
"purchase_price": 60
}
},
{
"id": 193,
"warehouse_id": 24,
"ingredient_id": 67,
"expiration_date": "2019-09-19",
"available_stocks": 43996,
"ingredient": {
"id": 67,
"name": "fwefe",
"SKU": "1000066",
"default_unit": {
"id": 26,
"name": "Milliliter"
},
"purchase_price": 70
}
},
{
"id": 52,
"outlet_item_id": null,
"warehouse_item_id": 191,
"ingredient_id": 65,
"quantity": 7,
"total_in_lowest": 0,
"stock_on_hand": 0,
"adjustment_price": 0,
"soh_total_in_lowest": 0,
"unit_price": 50,
"difference": 0,
"difference_in_lowest": 0
}
]
EXPECTED OUTPUT
[
{
"id": 191,
"warehouse_id": 24,
"ingredient_id": 65,
"expiration_date": "2019-07-31",
"available_stocks": 7,
"ingredient": {
"id": 65,
"name": "erg",
"SKU": "1000064",
"default_unit": {
"id": 26,
"name": "Milliliter"
},
"purchase_price": 50,
"quantity": 7,
"total_in_lowest": 0,
"stock_on_hand": 0,
"adjustment_price": 0,
"soh_total_in_lowest": 0,
"unit_price": 50,
"difference": 0,
"difference_in_lowest": 0
},
{
"id": 192,
"warehouse_id": 24,
"ingredient_id": 66,
"expiration_date": "2019-09-18",
"available_stocks": 33994,
"ingredient": {
"id": 66,
"name": "gvf",
"SKU": "1000065",
"default_unit": {
"id": 27,
"name": "Gram"
},
"purchase_price": 60
}
},
{
"id": 193,
"warehouse_id": 24,
"ingredient_id": 67,
"expiration_date": "2019-09-19",
"available_stocks": 43996,
"ingredient": {
"id": 67,
"name": "fwefe",
"SKU": "1000066",
"default_unit": {
"id": 26,
"name": "Milliliter"
},
"purchase_price": 70
}
},
{
"id": 52,
"outlet_item_id": null,
"warehouse_item_id": 191,
"ingredient_id": 65,
"quantity": 7,
"total_in_lowest": 0,
"stock_on_hand": 0,
"adjustment_price": 0,
"soh_total_in_lowest": 0,
"unit_price": 50,
"difference": 0,
"difference_in_lowest": 0
}
]
> EXPECTED OUTPUT
yes = [
{
"id": 191,
"warehouse_id": 24,
"ingredient_id": 65,
"expiration_date": "2019-07-31",
"available_stocks": 7,
"ingredient": {
"id": 65,
"name": "erg",
"SKU": "1000064",
"default_unit": {
"id": 26,
"name": "Milliliter"
},
"purchase_price": 50
}
},
{
"id": 192,
"warehouse_id": 24,
"ingredient_id": 66,
"expiration_date": "2019-09-18",
"available_stocks": 33994,
"ingredient": {
"id": 66,
"name": "gvf",
"SKU": "1000065",
"default_unit": {
"id": 27,
"name": "Gram"
},
"purchase_price": 60
}
},
{
"id": 193,
"warehouse_id": 24,
"ingredient_id": 67,
"expiration_date": "2019-09-19",
"available_stocks": 43996,
"ingredient": {
"id": 67,
"name": "fwefe",
"SKU": "1000066",
"default_unit": {
"id": 26,
"name": "Milliliter"
},
"purchase_price": 70
}
}
]