假設我有一個這樣的物件陣列:
[
{
"transactiondate.Display.V1.FormattedValue": "2/1/2016",
"transactiondate": "2016-02-01T08:00:00Z",
"level": "level 1",
"amount": "5"
},
{
"transactiondate.Display.V1.FormattedValue": "2/1/2017",
"transactiondate": "2017-02-01T08:00:00Z",
"level": "level 1",
"amount": "7"
},
{
"transactiondate.Display.V1.FormattedValue": "2/1/2017",
"transactiondate": "2017-02-01T08:00:00Z",
"level": "level 1",
"amount": "4"
},
{
"transactiondate.Display.V1.FormattedValue": "2/1/2016",
"transactiondate": "2016-02-01T08:00:00Z",
"level": "level 2",
"amount": "10"
},
{
"transactiondate.Display.V1.FormattedValue": "2/1/2016",
"transactiondate": "2016-02-01T08:00:00Z",
"level": "level 2",
"amount": "20"
},
{
"transactiondate.Display.V1.FormattedValue": "2/1/2017",
"transactiondate": "2017-02-01T08:00:00Z",
"level": "level 2",
"amount": "50"
},
...
]
我希望輸出看起來像這樣:
[
{
name: "level 1",
data: [5, 11] // Total amount in order of ascending year. 2016 had total amount of 5 and 2017 had total amount of 11
},
{
name: "level 2",
data: [30, 50]
},
{
name: "level x",
data: [...]
}
]
通過執行以下操作,我能夠成功地按年對其進行分組,但我不太確定如何獲取結果物件,然后將其轉換為所需的輸出。我必須遍歷物件的條目,按“級別”將其分成組,再次遍歷條目,然后使用減少來累積數量,然后再次將它們拆分為“名稱”和“資料”鍵/值對?如果我什至可以讓它發揮作用,我覺得這是非常低效的。任何幫助將非常感激。
var groupedByYr = data.reduce(function (r, a) {
r[a.transactiondate.substring(0,4)] = r[a.transactiondate.substring(0,4)] || [];
r[a.transactiondate.substring(0,4)].push(a);
return r;
});
uj5u.com熱心網友回復:
也許不是完美的答案,但我猜它有 1 個回圈:D
我試圖在代碼中盡可能多地解釋它
var data = [{
"transactiondate.Display.V1.FormattedValue": "2/1/2016",
"transactiondate": "2016-02-01T08:00:00Z",
"level": "level 1",
"amount": "5"
},
{
"transactiondate.Display.V1.FormattedValue": "2/1/2017",
"transactiondate": "2017-02-01T08:00:00Z",
"level": "level 1",
"amount": "7"
},
{
"transactiondate.Display.V1.FormattedValue": "2/1/2017",
"transactiondate": "2017-02-01T08:00:00Z",
"level": "level 1",
"amount": "4"
},
{
"transactiondate.Display.V1.FormattedValue": "2/1/2016",
"transactiondate": "2016-02-01T08:00:00Z",
"level": "level 2",
"amount": "10"
},
{
"transactiondate.Display.V1.FormattedValue": "2/1/2016",
"transactiondate": "2016-02-01T08:00:00Z",
"level": "level 2",
"amount": "20"
},
{
"transactiondate.Display.V1.FormattedValue": "2/1/2017",
"transactiondate": "2017-02-01T08:00:00Z",
"level": "level 2",
"amount": "50"
},
],
parse = [] //save parse array,
levels = {} //save level keys;
data.map((item) => {
let map = new Map(); // create a map for location of the value of the each year
if (typeof levels[item.level] == 'undefined') { // we didn't parse the level yet so it's new
map.set(item.transactiondate.substring(0, 4), 0); // set the location of the amount of the first year as 0 because it's the first
let key = parse.push({
name: item.level,
yearindexes: map,
data: [parseInt(item.amount)]
}); // save the parse array size as level key
levels[item.level] = key - 1; // save the level and index of it (key -1)
} else {
// parse the level before so it's exists
if (parse[levels[item.level]].yearindexes.has(item.transactiondate.substring(0, 4))) { // we have the year in the data
let yearindex = parse[levels[item.level]].yearindexes.get(item.transactiondate.substring(0, 4)); // get the index of the year
parse[levels[item.level]].data[yearindex] = parseInt(item.amount); // add the amount of the year to the index
} else {
// it's a new years
parse[levels[item.level]].data.push(parseInt(item.amount));// push the new year amount to the data
map = parse[levels[item.level]].yearindexes; // get previes year indexes
map.set(item.transactiondate.substring(0, 4), map.size); // add the new year with the size of yearindexes as the index of the new year
parse[levels[item.level]].yearindexes = map; // set the new yearindexes
}
}
});
//remove yearindexes from parse (optional)
const result = parse.map(({yearindexes,...rest}) => ({...rest}));
console.log(result);
uj5u.com熱心網友回復:
我認為您只需要分兩次完成,一次使用 reduce 將所有內容放入緩沖區,另一次使用 map 獲取該緩沖區并按照您的喜好對其進行格式化。我認為你可以一次性完成,但如果你不這樣做,閱讀起來會容易得多。
在這里,我使用reduce 回圈遍歷資料元素,并將buffer 作為引數傳遞給reduce,這樣我就可以繼續構建緩沖區物件。由于它是一個物件,我可以使用年份作為鍵(我從 Date 的 getYear() 中獲得)不斷添加每年的金額,然后當我完成后,我使用 Object.values 將物件映射到陣列.
如果我做對了,請告訴我。
const data = [
{
"transactiondate.Display.V1.FormattedValue": "2/1/2016",
"transactiondate": "2016-02-01T08:00:00Z",
"level": "level 1",
"amount": "5"
},
{
"transactiondate.Display.V1.FormattedValue": "2/1/2017",
"transactiondate": "2017-02-01T08:00:00Z",
"level": "level 1",
"amount": "7"
},
{
"transactiondate.Display.V1.FormattedValue": "2/1/2017",
"transactiondate": "2017-02-01T08:00:00Z",
"level": "level 1",
"amount": "4"
},
{
"transactiondate.Display.V1.FormattedValue": "2/1/2016",
"transactiondate": "2016-02-01T08:00:00Z",
"level": "level 2",
"amount": "10"
},
{
"transactiondate.Display.V1.FormattedValue": "2/1/2016",
"transactiondate": "2016-02-01T08:00:00Z",
"level": "level 2",
"amount": "20"
},
{
"transactiondate.Display.V1.FormattedValue": "2/1/2017",
"transactiondate": "2017-02-01T08:00:00Z",
"level": "level 2",
"amount": "50"
}
];
let buffer = {};
data.reduce((d, i)=>{
let t = new Date(i.transactiondate).getYear();
let amts = buffer[i.level]?.amts || {};
amts[t] = i.amount*1 (amts[t] || 0);
buffer[i.level] = {name: i.level, amts: amts};
}, buffer);
//console.log(buffer);
let final = Object.values(buffer).map(m=>{
return {name: m.name, data: Object.values(m.amts)};
});
console.log(final);
轉載請註明出處,本文鏈接:https://www.uj5u.com/caozuo/360556.html
標籤:javascript 数组 数据转换
