簡體   English   中英

在將(對象數組)列轉換為行數據時進行計算

[英]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},
]
  1. 日期必須是唯一的,
  2. Qlt 的值始終為(A 或 B 或 C),因此將其轉換為鍵名 QltA、QltB 和 QltC
  3. 所有其他(行、acc 和 tom)是相應 Qlt 的總和。

我已經嘗試了這篇文章的相反方法將行從 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);

  1. 創建日期集
  2. 為每個日期(集合中的元素)添加一個對象
  3. 使用 for 循環用輸入中的值填充對象

 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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM