我有一個來自 DB 的結果集,如下所示:
"result": [
{
"customerId": "20572696",
"totalIncome": "260000",
"total_Expense": "130000",
"relationName": "VIJAYA",
"relationDOB": "23839",
"relation": "Daughter"
},
{
"customerId": "20572696",
"totalIncome": "260000",
"total_Expense": "130000",
"relationName": "Riyaz",
"relationDOB": "26665",
"relation": "SPOUSE"
},
{
"customerId": "20570000",
"totalIncome": "200000",
"total_Expense": "100000",
"relationName": "John",
"relationDOB": "26000",
"relation": "SON"
}
]
customerId
我需要通過將,分組并將其余物件放在名為 的陣列中來格式化結果集totalIncome
,如下所示。total_Expense
relation
"result": [{
"customerId": "20572696",
"totalIncome": "260000",
"total_Expense": "130000",
"relations": [
{
"relationName": "VIJAYA"
"relationDOB": "23839 ",
"relation": "Daughter "
},
{
"relationName": "Riyaz",
"relationDOB": "26665",
"relation": "SPOUSE"
}
]
},
{
"customerId": "20570000",
"totalIncome": "200000",
"total_Expense": "100000",
"relations": [
{
"relationName": "John"
"relationDOB": "26000",
"relation": "SON"
}]
}
]
我想到的一種方法是在一個單獨的陣列中過濾所有不同的 customerId 并回圈遍歷結果陣列并分離前三個欄位并將剩余欄位放在一個物件中并將其推送到子陣列中。但是有什么優雅的方法可以做到這一點嗎?
uj5u.com熱心網友回復:
reduce
你可以用和做這樣的事情Object.values
基本上使用reduce創建一個以customerId為鍵的物件
然后使用 Object.values 你擺脫了 customerId 索引,你得到你的結果作為一個陣列
請注意,由于20570000
小于20572696
回傳陣列時的順序是相反的
const group = data => Object.values(data.reduce((res, {customerId, totalIncome, total_Expense, ...rest}) => {
const existing = res[customerId] || {customerId, totalIncome, total_Expense, relations:[]}
return {
...res,
[customerId]: {
...existing,
relations: [...existing.relations, rest]
}
}
}, {}))
const data = [
{
"customerId": "20572696",
"totalIncome": "260000",
"total_Expense": "130000",
"relationName": "VIJAYA",
"relationDOB": "23839",
"relation": "Daughter"
},
{
"customerId": "20572696",
"totalIncome": "260000",
"total_Expense": "130000",
"relationName": "Riyaz",
"relationDOB": "26665",
"relation": "SPOUSE"
},
{
"customerId": "20570000",
"totalIncome": "200000",
"total_Expense": "100000",
"relationName": "John",
"relationDOB": "26000",
"relation": "SON"
}
]
console.log(group(data))
uj5u.com熱心網友回復:
如果customerId
,totalIncome
和total_Expense
都相同,則:
const obj = {"result":[{"customerId":"20572696","totalIncome":"260000","total_Expense":"130000","relationName":"VIJAYA","relationDOB":"23839","relation":"Daughter"},{"customerId":"20572696","totalIncome":"260000","total_Expense":"130000","relationName":"Riyaz","relationDOB":"26665","relation":"SPOUSE"},{"customerId":"20570000","totalIncome":"200000","total_Expense":"100000","relationName":"John","relationDOB":"26000","relation":"SON"}]};
let relations = {};
let identifierFields = {};
obj.result.forEach(({ customerId, totalIncome, total_Expense, ...rest }) => {
relations[customerId] = (relations[customerId] || []);
relations[customerId].push({ ...rest });
if (!identifierFields[customerId]) identifierFields[customerId] = { customerId, totalIncome, total_Expense };
});
const result = Object.values(identifierFields).map((r, idx) => ({
...r,
relations: relations[r.customerId]
}));
console.log(result);
.as-console-wrapper { max-height: 100% !important; top: auto; }
這比簡單地傳播所有內容更高效,因為它只傳播物件中的其他屬性,而其他push
所有內容都傳播,從而為您節省大量記憶體和時間損失。
uj5u.com熱心網友回復:
使用forEach
和構建一個物件,其鍵為 customerId,值聚合為相同的 id。
const process = (data, track = {}) => {
data.forEach(({ customerId, totalIncome, total_Expense, ...item }) => {
if (!track[customerId]) {
track[customerId] = {
customerId,
totalIncome,
total_Expense,
relations: [item],
};
} else {
track[customerId].relations.push(item);
}
});
return Object.values(track);
};
const data =[{"customerId":"20572696","totalIncome":"260000","total_Expense":"130000","relationName":"VIJAYA","relationDOB":"23839","relation":"Daughter"},{"customerId":"20572696","totalIncome":"260000","total_Expense":"130000","relationName":"Riyaz","relationDOB":"26665","relation":"SPOUSE"},{"customerId":"20570000","totalIncome":"200000","total_Expense":"100000","relationName":"John","relationDOB":"26000","relation":"SON"}];
console.log(process(data));
.as-console-wrapper { max-height: 100% !important; top: auto; }
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/488756.html
標籤:javascript 数组 目的 ecmascript-6