從下面的物件中,我想通過它們的鍵組(1345、2353)總結某些屬性(票價 稅和 com agency)并在當前陣列物件中更新它。
var details = [{ 1345:[
{route: 34, fare: 45, tax: 46, date: 46, com: 45, agency: 24, totalCost: 0, totalFee: 0}],
2353: [
{route: 32, fare: 45, tax: 45, date: 56, com: 34, agency: 52, totalCost: 0, totalFee: 0},
{route: 42, fare: 34, tax: 64, date: 34, com: 56, agency: 34, totalCost: 0, totalFee: 0}
]}
]
預期輸出:更新details(totalCost 和 totalFee)
1345:
{ route: 34, fare: 45, .... totalCost: 91, totalFee: 69 }
2353:
{ route: 32, fare: 45, ... totalCost: 188, totalFee: 90 },
{ route: 42, fare: 34, ... totalCost: 188, totalFee: 176 }
totalCost = fare tax和totalFee = com agency
我試圖簡化陣列物件并使用 Object.entries(details[0]) 進行轉換,然后減少以總結目標屬性。
Object.entries(details[0]).reduce((acc, curr) => (acc = acc curr["fare"] curr["tax"]), 0);
但是,回傳了 NaN。
如果有人能告訴我如何遍歷每個關鍵組并總結目標值并更新它(totalCost 和 totalFee),我將不勝感激。
uj5u.com熱心網友回復:
一種(許多)方法是以totals您想要的格式初始化物件。
const totals = {}
for (const key in details[0]) {
totals[key] = {
totalCost: 0,
totalFee: 0,
}
}
然后使用一個簡單的 sum 函式對任意值陣列求和:
function sum(...nums) {
return nums.reduce((acc, val) => acc val)
}
其余的很簡單:根據您的totalCost/totalFee邏輯求和:
for (const [key, vals] of Object.entries(details[0])) {
totals[key].totalCost = sum(...vals.map(val => val.fare val.tax))
totals[key].totalFee = sum(...vals.map(val => val.com val.agency))
}
這是整個shebang:
const details = [
{
1345: [
{route: 34, fare: 45, tax: 46, date: 46, com: 45, agency: 24, totalCost: 0, totalFee: 0},
],
2353: [
{route: 32, fare: 45, tax: 45, date: 56, com: 34, agency: 52, totalCost: 0, totalFee: 0},
{route: 42, fare: 34, tax: 64, date: 34, com: 56, agency: 34, totalCost: 0, totalFee: 0},
],
},
]
const totals = {}
for (const key in details[0]) {
totals[key] = {
totalCost: 0,
totalFee: 0,
}
}
for (const [key, vals] of Object.entries(details[0])) {
totals[key].totalCost = sum(...vals.map(val => val.fare val.tax))
totals[key].totalFee = sum(...vals.map(val => val.com val.agency))
}
console.log(totals)
function sum(...nums) {
return nums.reduce((acc, val) => acc val)
}
uj5u.com熱心網友回復:
我們可以通過幾次Array.reduce()呼叫來做到這一點,最終結果應該符合要求。
對于每個鍵組,我們將Object.entries()用于獲取組的鍵和值。
const details = [{ 1345:[ {route: 34, fare: 45, tax: 46, date: 46, com: 45, agency: 24, totalCost: 0, totalFee: 0}], 2353: [ {route: 32, fare: 45, tax: 45, date: 56, com: 34, agency: 52, totalCost: 0, totalFee: 0}, {route: 42, fare: 34, tax: 64, date: 34, com: 56, agency: 34, totalCost: 0, totalFee: 0} ]} ]
const result = details.reduce((acc, group) => {
return Object.entries(group).reduce((acc, [key, routes] ) => {
return routes.reduce((acc, { fare, tax, com, agency}) => {
acc[key] = acc[key] || {};
acc[key].totalCost = (acc[key].totalCost || 0) fare tax;
acc[key].totalFee = (acc[key].totalFee || 0) com agency;
return acc;
}, acc)
}, acc)
}, {})
console.log('Result:', result)
.as-console-wrapper { max-height: 100% !important; }
我在這里更新了每個鍵組下的原始陣列,將其命名為“路由”,這可以更改為任何內容:
const details = [{ 1345:[ {route: 34, fare: 45, tax: 46, date: 46, com: 45, agency: 24, totalCost: 0, totalFee: 0}], 2353: [ {route: 32, fare: 45, tax: 45, date: 56, com: 34, agency: 52, totalCost: 0, totalFee: 0}, {route: 42, fare: 34, tax: 64, date: 34, com: 56, agency: 34, totalCost: 0, totalFee: 0} ]} ]
const result = details.reduce((acc, group) => {
return Object.entries(group).reduce((acc, [key, routes] ) => {
return routes.reduce((acc, { fare, tax, com, agency}) => {
acc[key] = acc[key] || { routes };
acc[key].totalCost = (acc[key].totalCost || 0) fare tax;
acc[key].totalFee = (acc[key].totalFee || 0) com agency;
return acc;
}, acc)
}, acc)
}, {})
console.log('Result:', result)
.as-console-wrapper { max-height: 100% !important; }
uj5u.com熱心網友回復:
擴大使用范圍Object.values
var details = [{ 1345:[
{route: 34, fare: 45, tax: 46, date: 46, com: 45, agency: 24, totalCost: 0, totalFee: 0}],
2353: [
{route: 32, fare: 45, tax: 45, date: 56, com: 34, agency: 52, totalCost: 0, totalFee: 0},
{route: 42, fare: 34, tax: 64, date: 34, com: 56, agency: 34, totalCost: 0, totalFee: 0}
]}
]
Object.values(details).map(x => {
details = {}
let count = 0;
for (let y of Object.values(x)) {
y = y.reduce(function (a, b) {
for (const key in b) {
if (a[key]) {
a[key] = a[key] b[key]
} else {
a[key] = b[key];
}
}
return a;
}, {});
y['totalCost'] = y['fare'] y['tax'];
y['totalFee'] = y['com'] y['agency'];
let totalObj = {};
totalObj['totalCost:'] = y['totalCost'];
totalObj['totalFee:'] = y['totalFee'];
details[`${Object.keys(x)[count]}`] = totalObj;
count ;
};
});
console.log(details);
轉載請註明出處,本文鏈接:https://www.uj5u.com/caozuo/504555.html
標籤:javascript 数组 目的 减少
