我目前有一個看起來像這樣的物件陣列:
const orders = [
{ id: '1', userId: '3', total: 300 },
{ id: '2', userId: '4', total: 200 },
{ id: '3', userId: '5', total: 101 },
{ id: '4', userId: '6', total: 80 },
{ id: '5', userId: '7', total: 76 },
{ id: '6', userId: '8', total: 44 },
{ id: '7', userId: '9', total: 1000 },
{ id: '8', userId: '10', total: 99 },
{ id: '9', userId: '3', total: 65 },
{ id: '10', userId: '4', total: 22 }
];
我將如何組合陣列中共享相同 userId 的任何物件的總數?
我希望我的結果是這樣的:
const newOrdersArray = [
{ id: '1', userId: '3', total: 365 },
{ id: '2', userId: '4', total: 222 },
{ id: '3', userId: '5', total: 101 },
{ id: '4', userId: '6', total: 80 },
{ id: '5', userId: '7', total: 76 },
{ id: '6', userId: '8', total: 44 },
{ id: '7', userId: '9', total: 1000 },
{ id: '8', userId: '10', total: 99 }
];
uj5u.com熱心網友回復:
我不知道這背后的用例,但查看資料,我假設id是特定訂單的id。將其保留在累積結果中感覺有些奇怪,其中總數僅與用戶的所有訂單有關,而不再與單個訂單有關。因此,我會放棄它并像這樣計算總數:
totals = orders.reduce((totals, order) => {
totals[order.userId] = (totals[order.userId] ?? 0) order.total;
return totals;
}, {});
是的,您不再有陣列,而是有一個以 userIds 作為鍵的物件。如果這不適合您的特定用例,只需再次轉換它。
uj5u.com熱心網友回復:
這是一種使用reduce的方法
const orders = [
{ id: '1', userId: '3', total: 300 },
{ id: '2', userId: '4', total: 200 },
{ id: '3', userId: '5', total: 101 },
{ id: '4', userId: '6', total: 80 },
{ id: '5', userId: '7', total: 76 },
{ id: '6', userId: '8', total: 44 },
{ id: '7', userId: '9', total: 1000 },
{ id: '8', userId: '10', total: 99 },
{ id: '9', userId: '3', total: 65 },
{ id: '10', userId: '4', total: 22 }
];
let combined = orders.reduce((b, a) => {
let index = b.findIndex(arr => arr.userId == a.userId);
delete a.id
if (index > -1) b[index].total = a.total;
else b.push(a);
return b;
}, [])
console.log(combined);
uj5u.com熱心網友回復:
嘗試這個:
const orders = [
{ id: '1', userId: '3', total: 300 },
{ id: '2', userId: '4', total: 200 },
{ id: '3', userId: '5', total: 101 },
{ id: '4', userId: '6', total: 80 },
{ id: '5', userId: '7', total: 76 },
{ id: '6', userId: '8', total: 44 },
{ id: '7', userId: '9', total: 1000 },
{ id: '8', userId: '10', total: 99 },
{ id: '9', userId: '3', total: 65 },
{ id: '10', userId: '4', total: 22 }
];
const totals = [];
orders.forEach(x => {
const obj = totals.find(o => o.userId === x.userId);
if (obj) {
obj.total = obj.total x.total;
} else {
totals.push(x);
}
});
console.log(totals);
uj5u.com熱心網友回復:
您可以使用一個物件進行尋址,userId并從該物件中獲取值作為結果。
const
orders = [{ id: '1', userId: '3', total: 300 }, { id: '2', userId: '4', total: 200 }, { id: '3', userId: '5', total: 101 }, { id: '4', userId: '6', total: 80 }, { id: '5', userId: '7', total: 76 }, { id: '6', userId: '8', total: 44 }, { id: '7', userId: '9', total: 1000 }, { id: '8', userId: '10', total: 99 }, { id: '9', userId: '3', total: 65 }, { id: '10', userId: '4', total: 22 }],
result = Object.values(orders.reduce((r, o) => {
r[o.userId] ??= { ...o, total: 0 };
r[o.userId].total = o.total;
return r;
}, {}));
console.log(result);
.as-console-wrapper { max-height: 100% !important; top: 0; }
uj5u.com熱心網友回復:
以下是.reduce()將 ID 收集在每個用戶 ID 的陣列中的方法的一種變體:
const orders = [
{ id: '1', userId: '3', total: 300 },
{ id: '2', userId: '4', total: 200 },
{ id: '3', userId: '5', total: 101 },
{ id: '4', userId: '6', total: 80 },
{ id: '5', userId: '7', total: 76 },
{ id: '6', userId: '8', total: 44 },
{ id: '7', userId: '9', total: 1000 },
{ id: '8', userId: '10', total: 99 },
{ id: '9', userId: '3', total: 65 },
{ id: '10', userId: '4', total: 22 }
];
let combined = Object.values(orders.reduce((a,c) => {
let e = (a[c.userId] = a[c.userId] || {ids:[],userId:c.userId,total:0});
e.total =c.total;
e.ids.push(c.id);
return a;
}, {}))
console.log(combined);
轉載請註明出處,本文鏈接:https://www.uj5u.com/qukuanlian/371119.html
標籤:javascript 数组 目的
