我从加密交易的CSV文件中提取了数据,并使用Token列对结果进行了分组。以下是分组数组看起来像的样子
**[
ETH:[
[ 1571967200, 'DEPOSIT', 'ETH', 0.68364 ],
[ 1571967189, 'WITHDRAWAL', 'ETH', 0.493839 ],
[ 1571967110, 'DEPOSIT', 'ETH', 0.347595 ],
[ 1571966982, 'WITHDRAWAL', 'ETH', 0.266166 ],
[ 1571966641, 'DEPOSIT', 'ETH', 0.899781 ],
[ 1571966421, 'DEPOSIT', 'ETH', 0.218207 ],
[ 1571966410, 'DEPOSIT', 'ETH', 0.205472 ],
[ 1571966250, 'WITHDRAWAL', 'ETH', 0.761543 ],
[ 1571966124, 'DEPOSIT', 'ETH', 0.66315 ],
[ 1571965892, 'DEPOSIT', 'ETH', 0.554933 ]
],
BTC: [
[ 1571966685, 'DEPOSIT', 'BTC', 0.658794 ],
[ 1571966568, 'DEPOSIT', 'BTC', 0.630386 ],
[ 1571966566, 'DEPOSIT', 'BTC', 0.985879 ],
[ 1571966499, 'DEPOSIT', 'BTC', 0.162165 ],
[ 1571966329, 'WITHDRAWAL', 'BTC', 0.063663 ],
[ 1571966194, 'DEPOSIT', 'BTC', 0.858688 ],
[ 1571966049, 'DEPOSIT', 'BTC', 0.696682 ],
[ 1571966026, 'DEPOSIT', 'BTC', 0.747093 ],
[ 1571965990, 'WITHDRAWAL', 'BTC', 0.987358 ]
]
]**
这是我用来分组的代码
inputStream.pipe(new CsvReadableStream({ parseNumbers: true, parseBooleans: true, trim: true }))
.on('data', (row)=> {
csvData.push(row);
})
.on('end', function () {
groupedResult = csvData.reduce((acc, curr) => {
if(!acc[curr[2]]){
acc[curr[2]] = [];
}
acc[curr[2]].push(curr);
return acc;
},[]);
});
我正试着把所有的存款加在一起,并对每组的提款进行细分。下面是我用来将withdrwals和存款组合在一起的代码,但不幸的是,它不起作用。我可能做错了什么,或者我如何添加所有存款并减去每个代币的withdrwals?
for(groupName in groupedResult){
groupedResult[groupName].reduce((acc, curr)=>{
acc[curr[1]] = curr[1];
},[])
}
这是我得到的错误
acc[curr[1]] = curr[1];
^
TypeError: Cannot set property 'WITHDRAWAL' of undefined
at E:Command Linebinindex.js:46:34
at Array.reduce (<anonymous>)
这是我想要得到的结果
[
ETH:[
[
Balance['Value here']
],
],
BTC: [
[
Balance['value here']
],
]
]
;减速器";Array#reduce(reducer, initValue, ...)
的回调必须返回累加器值(return acc;
)以便在下一轮中使用
这是因为您可能必须处理不保持一致引用的基元值,例如,在您想要对数字列表求和的情况下:[1, 2, 3, 4].reduce((number,acc) => {return number+acc;}, 0)
const groupedResult = {
ETH:[
[ 1571967200, 'DEPOSIT', 'ETH', 0.68364 ],
[ 1571967189, 'WITHDRAWAL', 'ETH', 0.493839 ],
[ 1571967110, 'DEPOSIT', 'ETH', 0.347595 ],
[ 1571966982, 'WITHDRAWAL', 'ETH', 0.266166 ],
[ 1571966641, 'DEPOSIT', 'ETH', 0.899781 ],
[ 1571966421, 'DEPOSIT', 'ETH', 0.218207 ],
[ 1571966410, 'DEPOSIT', 'ETH', 0.205472 ],
[ 1571966250, 'WITHDRAWAL', 'ETH', 0.761543 ],
[ 1571966124, 'DEPOSIT', 'ETH', 0.66315 ],
[ 1571965892, 'DEPOSIT', 'ETH', 0.554933 ]
],
BTC: [
[ 1571966685, 'DEPOSIT', 'BTC', 0.658794 ],
[ 1571966568, 'DEPOSIT', 'BTC', 0.630386 ],
[ 1571966566, 'DEPOSIT', 'BTC', 0.985879 ],
[ 1571966499, 'DEPOSIT', 'BTC', 0.162165 ],
[ 1571966329, 'WITHDRAWAL', 'BTC', 0.063663 ],
[ 1571966194, 'DEPOSIT', 'BTC', 0.858688 ],
[ 1571966049, 'DEPOSIT', 'BTC', 0.696682 ],
[ 1571966026, 'DEPOSIT', 'BTC', 0.747093 ],
[ 1571965990, 'WITHDRAWAL', 'BTC', 0.987358 ]
]
};
const groupedBalance = {};
for(groupName in groupedResult) {
const accumulated = groupedResult[groupName].reduce((acc, curr)=>{
acc[curr[1]] += curr[3];
return acc; // ← return the accumulator to use for the next round
}, {DEPOSIT: 0, WITHDRAWAL: 0});
console.log(groupName, accumulated);
groupedBalance[groupName] = {
Balance: accumulated['DEPOSIT'] - accumulated['WITHDRAWAL']
};
}
console.log('FINAL RESULTS:', groupedBalance);