![](/img/trans.png)
[英]Converting an object in an array of objects into an array, while preserving other data
[英]Calculating while converting (array of objects) columns to row data
我有以下數組:
var test =[
{Date:"2020-06-01", Type:"ZT", Qlt:"A",line:100,acc:1000,tom:10000},
{Date:"2020-06-01", Type:"ZT", Qlt:"B",line:200,acc:2000,tom:20000},
{Date:"2020-06-01", Type:"ZT", Qlt:"C",line:300,acc:3000,tom:30000},
{Date:"2020-06-02", Type:"ZT", Qlt:"A",line:400,acc:4000,tom:40000},
{Date:"2020-06-02", Type:"ZT", Qlt:"B",line:500,acc:5000,tom:50000},
{Date:"2020-06-02", Type:"ZT", Qlt:"C",line:600,acc:6000,tom:60000}
]
我想要達到的是這個結果:
var result=[
{Date:"2020-06-01", Type:"ZT", QltA:100, QltB:200, QltC:300, totalQlt:600, totalAcc:6000, totalTom:60000},
{Date:"2020-06-02", Type:"ZT", QltA:400, QltB:500, QltC:600, totalQlt:1500, totalAcc:15000, totalTom:150000},
]
我已經嘗試了這篇文章的相反方法將行從 javascript 數組轉換為列,但我沒有設法讓它工作,而且我在計算總和時遇到了麻煩。
我知道這里有些棘手,如果您能提供解決方案和分類解釋以理解邏輯,那就太好了?
任何幫助將不勝感激,謝謝。
這只是Date
屬性的group by
操作。 這里用Array#reduce()
。
如果需要,您可以用 OR 短路替換無效分配 (??=) 。
a[Date] ??= { Date, Type, ...};
// or
a[Date] || (a[Date] = { Date, Type, ...});
const input = [{ Date: "2020-06-01", Type: "ZT", Qlt: "A", line: 100, acc: 1000, tom: 10000 }, { Date: "2020-06-01", Type: "ZT", Qlt: "B", line: 200, acc: 2000, tom: 20000 }, { Date: "2020-06-01", Type: "ZT", Qlt: "C", line: 300, acc: 3000, tom: 30000 }, { Date: "2020-06-02", Type: "ZT", Qlt: "A", line: 400, acc: 4000, tom: 40000 }, { Date: "2020-06-02", Type: "ZT", Qlt: "B", line: 500, acc: 5000, tom: 50000 }, { Date: "2020-06-02", Type: "ZT", Qlt: "C", line: 600, acc: 6000, tom: 60000 }], result = Object .values(input .reduce((a, { Date, Type, Qlt, line, acc, tom }) => { a[Date] ??= { Date, Type, totalQlt: 0, totalAcc: 0, totalTom: 0 }; a[Date][`Qlt${Qlt}`] = line; a[Date].totalQlt += line; a[Date].totalAcc += acc; a[Date].totalTom += tom; return a; }, {}) ); console.log(result)
.as-console-wrapper { max-height: 100% !important; top: 0; }
const data = [ { Date:"2020-06-01", Type:"ZT", Qlt:"A", line:100, acc:1000, tom:10000 }, { Date:"2020-06-01", Type:"ZT", Qlt:"B", line:200, acc:2000, tom:20000 }, { Date:"2020-06-01", Type:"ZT", Qlt:"C", line:300, acc:3000, tom:30000 }, { Date:"2020-06-02", Type:"ZT", Qlt:"A", line:400, acc:4000, tom:40000 }, { Date:"2020-06-02", Type:"ZT", Qlt:"B", line:500, acc:5000, tom:50000 }, { Date:"2020-06-02", Type:"ZT", Qlt:"C", line:600, acc:6000, tom:60000 } ]; const result = [... // iterate over the list while updating a Map of Date-Items data.reduce((map, { Date, Type, Qlt, line, acc, tom }) => { // get value of Date from map if exists const prev = map.get(Date) || {}; // set/update map values map.set( Date, { ...prev, Date, Type, [`Qlt${Qlt}`]: (prev.line || 0 ) + line, totalQlt: (prev.totalQlt || 0) + line, totalAcc: (prev.totalAcc || 0) + acc, totalTom: (prev.totalTom || 0) + tom } ); return map; }, new Map) // return updated items of same dates .values() ]; console.log(result);
var test =[ {date:"2020-06-01", Type:"ZT", Qlt:"A",line:100,acc:1000,tom:10000}, {date:"2020-06-01", Type:"ZT", Qlt:"B",line:200,acc:2000,tom:20000}, {date:"2020-06-01", Type:"ZT", Qlt:"C",line:300,acc:3000,tom:30000}, {date:"2020-06-02", Type:"ZT", Qlt:"A",line:400,acc:4000,tom:40000}, {date:"2020-06-02", Type:"ZT", Qlt:"B",line:500,acc:5000,tom:50000}, {date:"2020-06-02", Type:"ZT", Qlt:"C",line:600,acc:6000,tom:60000} ] function resOBj(date,Type,QltA,QltB,QltC,totalQlt,totalAcc,totalTom){ this.date=date, this.Type=Type, this.QltA=QltA, this.QltB=QltB, this.QltC=QltC, this.totalQlt=totalQlt, this.totalAcc=totalAcc, this.totalTom=totalTom } convertData=(test)=>{ if (test===undefined){ return test } let result=[] let setDates= new Set() test.forEach(t => { setDates.add(t.date) }) for(const value of setDates){ result.push(new resOBj(value,"",0,0,0,0,0,0)) for (let i=0;i<test.length;i++){ if (test[i].date===value){ result[result.length-1].Type = test[i].Type result[result.length-1].QltA = test[i].Qlt==="A"?test[i].line:result[result.length-1].QltA result[result.length-1].QltB = test[i].Qlt==="B"?test[i].line:result[result.length-1].QltB result[result.length-1].QltC = test[i].Qlt==="C"?test[i].line:result[result.length-1].QltC result[result.length-1].totalQlt += test[i].line result[result.length-1].totalAcc += test[i].acc result[result.length-1].totalTom += test[i].tom } } } return result } console.log(convertData(test))
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.