获取分组数组的 2 个索引之间的差异 (JavaScript)

Get diffrence between 2 indexes of a grouped Array (JavaScript)

我已从加密交易的 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;

            },[]);

        });


我正在尝试将所有存款相加并减去每组的提款。下面是我用来将取款和存款组合在一起的代码,但不幸的是它不起作用。我可能做错了什么或者我如何添加所有存款并减去每个代币的取款?

     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 Line\bin\index.js:46:34
    at Array.reduce (<anonymous>)

这是我想要得到的预期结果

[
  ETH:[
    [
     Balance['Value here'] 
    ],      
  ],
 BTC: [
    [
     Balance['value here'] 
    ],
  ]
]

Array#reduce(reducer, initValue, ...) 的“reducer”回调必须 return 下一轮使用的累加器值 (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);