【发布时间】:2021-03-17 20:15:45
【问题描述】:
我需要一个最佳解决方案来遍历每个订单项目,如果有两个产品 ID 匹配,则添加数量:
var order = [
{
"coupon": {},
"billingAddress": {},
"shippingAddress": {},
"_id": "1234567890s4s5s3",
"userId": 125,
"subtotal": 15208,
"shipping": null,
"tax": 192,
"total": 7700,
"status": "confirmed",
"orderItems": [
{
"productId": "5f44eb9a491a455bc6de2de5",
"productTitle": "iPhone",
"quantity": 1,
"price": 3104,
"tax": 96,
"subtotal": 3104
},
{
"productId": "5f44eb9a491a455bc6de2de5",
"productTitle": "Iphone",
"quantity": 1,
"price": 3104,
"tax": 96,
"subtotal": 3104
},
{
"productId": "5fc0b9646ce7332838122784",
"productTitle": "Tv",
"quantity": 1,
"price": 4500,
"subtotal": 4500,
"tax": 0
},
{
"productId": "5fc0b9646ce7332838122784",
"productTitle": "Tv",
"quantity": 1,
"price": 4500,
"subtotal": 4500,
"tax": 0
}
],
"shipmentTracking": [
{}
],
"siteId": "5f3799e12c6291446ea8c75f",
},
{
"billingAddress": {},
"shippingAddress": {},
"_id": "5f9d5bead92083a9810d526c",
"status": "confirmed",
"userId": 125,
"subtotal": 121.25,
"shipping": 5,
"total": 130,
"orderItems": [
{
"productId": "5f44eb9a491a455bc6de2de5",
"productTitle": "Iphone",
"quantity": 1,
"price": 121.25,
"subtotal": 121.25,
"shipping": 5,
"tax": 3.75
}
],
"siteId": "5f3799e12c6291446ea8c75f",
}]
请在这种情况下查看上面的代码如何获得如下输出:
数据:[{ 产品编号:5fc0b9646ce7332838122784, 产品名称:电视, 总数量:2, totalSaleAmount:--
},{产品 2 详细信息 }]
【问题讨论】:
标签: javascript node.js arrays object