每个 id 的嵌套对象总和
Sum in nested object for each id
我需要根据 'fromSuppliers' 属性中的 id 对产品数组属性中的所有 'price' 求和。最好的方法是什么?
我尝试过使用 map 和 reduce,但没有成功。任何帮助将不胜感激。
此示例的预期输出:
[
{ id: 1, totalPrice: 19,84 }
{ id: 2, totalPrice: 9.84 }
]
对象:
const Jsonresult = {
items: {
'**9089**': {
createdAt: '2021-02-11T17:25:22.960-03:00',
product: [{
fromSuppliers: {
productSupplier: {
stock: 102,
promoPrice: 16,
},
'**id**': 2
}
}],
total: 9.84,
quantity: 1,
price: '**9.84**',
updatedAt: '2021-02-11T17:25:22.960-03:00'
},
'**9090**': {
createdAt: '2021-02-11T17:25:22.960-03:00',
product: [{
fromSuppliers: {
productSupplier: {
stock: 102,
promoPrice: 7,
},
'**id**': 1
}
}],
total: 9.84,
quantity: 1,
'**price**': 9.84,
updatedAt: '2021-02-11T17:25:22.960-03:00'
},
'**9091**': {
createdAt: '2021-02-11T17:25:22.960-03:00',
product: [{
fromSuppliers: {
productSupplier: {
stock: 102,
promoPrice: 7,
},
'**id**': 1
}
}],
total: 9.84,
quantity: 1,
'**price**': 10,
updatedAt: '2021-02-11T17:25:22.960-03:00'
},
}
您可以使用 Object.values()
来获取您的 items
的数组。然后我们将使用 Array.reduce()
创建一个地图对象,键入 id
。我们将再次使用 Object.values 将地图对象转换为数组。
const Jsonresult = { items: { '9089': { createdAt: '2021-02-11T17:25:22.960-03:00', product: [{ fromSuppliers: { productSupplier: { stock: 102, promoPrice: 16, }, id: 2 } }], total: 9.84, quantity: 1, price: 9.84, updatedAt: '2021-02-11T17:25:22.960-03:00' }, '9090': { createdAt: '2021-02-11T17:25:22.960-03:00', product: [{ fromSuppliers: { productSupplier: { stock: 102, promoPrice: 7, }, id: 1 } }], total: 9.84, quantity: 1, price: 9.84, updatedAt: '2021-02-11T17:25:22.960-03:00' }, '9091': { createdAt: '2021-02-11T17:25:22.960-03:00', product: [{ fromSuppliers: { productSupplier: { stock: 102, promoPrice: 7, }, id: 1 } }], total: 9.84, quantity: 1, price: 10, updatedAt: '2021-02-11T17:25:22.960-03:00' }, } };
const items = Object.values(Jsonresult.items);
const result = Object.values(items.reduce((acc, { price, product: [ { fromSuppliers: { id }} ]}) => {
if (!acc[id]) acc[id] = { id, price: 0 };
acc[id].price += price;
return acc;
}, {}));
console.log(result)
.as-console-wrapper { max-height: 100% !important; top: 0; }
我需要根据 'fromSuppliers' 属性中的 id 对产品数组属性中的所有 'price' 求和。最好的方法是什么? 我尝试过使用 map 和 reduce,但没有成功。任何帮助将不胜感激。
此示例的预期输出:
[
{ id: 1, totalPrice: 19,84 }
{ id: 2, totalPrice: 9.84 }
]
对象:
const Jsonresult = {
items: {
'**9089**': {
createdAt: '2021-02-11T17:25:22.960-03:00',
product: [{
fromSuppliers: {
productSupplier: {
stock: 102,
promoPrice: 16,
},
'**id**': 2
}
}],
total: 9.84,
quantity: 1,
price: '**9.84**',
updatedAt: '2021-02-11T17:25:22.960-03:00'
},
'**9090**': {
createdAt: '2021-02-11T17:25:22.960-03:00',
product: [{
fromSuppliers: {
productSupplier: {
stock: 102,
promoPrice: 7,
},
'**id**': 1
}
}],
total: 9.84,
quantity: 1,
'**price**': 9.84,
updatedAt: '2021-02-11T17:25:22.960-03:00'
},
'**9091**': {
createdAt: '2021-02-11T17:25:22.960-03:00',
product: [{
fromSuppliers: {
productSupplier: {
stock: 102,
promoPrice: 7,
},
'**id**': 1
}
}],
total: 9.84,
quantity: 1,
'**price**': 10,
updatedAt: '2021-02-11T17:25:22.960-03:00'
},
}
您可以使用 Object.values()
来获取您的 items
的数组。然后我们将使用 Array.reduce()
创建一个地图对象,键入 id
。我们将再次使用 Object.values 将地图对象转换为数组。
const Jsonresult = { items: { '9089': { createdAt: '2021-02-11T17:25:22.960-03:00', product: [{ fromSuppliers: { productSupplier: { stock: 102, promoPrice: 16, }, id: 2 } }], total: 9.84, quantity: 1, price: 9.84, updatedAt: '2021-02-11T17:25:22.960-03:00' }, '9090': { createdAt: '2021-02-11T17:25:22.960-03:00', product: [{ fromSuppliers: { productSupplier: { stock: 102, promoPrice: 7, }, id: 1 } }], total: 9.84, quantity: 1, price: 9.84, updatedAt: '2021-02-11T17:25:22.960-03:00' }, '9091': { createdAt: '2021-02-11T17:25:22.960-03:00', product: [{ fromSuppliers: { productSupplier: { stock: 102, promoPrice: 7, }, id: 1 } }], total: 9.84, quantity: 1, price: 10, updatedAt: '2021-02-11T17:25:22.960-03:00' }, } };
const items = Object.values(Jsonresult.items);
const result = Object.values(items.reduce((acc, { price, product: [ { fromSuppliers: { id }} ]}) => {
if (!acc[id]) acc[id] = { id, price: 0 };
acc[id].price += price;
return acc;
}, {}));
console.log(result)
.as-console-wrapper { max-height: 100% !important; top: 0; }