0

I have extracted data from a CSV file of a crypto transaction and grouped the results using the Token column. Here is how the grouped array looks like

 **[
  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 ]
  ]
]**

Here is the code i've used to group

   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;

            },[]);

        });


I'm trying to add all the deposits together and subrtact the withdrawals for each group. Below is the code i'm using to group the withdrwals together and also the deposits but unfortunately it does not work. What could i be doing wrong or how do I add all the deposits and subtract the withdrwals for each token?

     for(groupName in groupedResult){
                groupedResult[groupName].reduce((acc, curr)=>{
                    acc[curr[1]] = curr[1];
                                        
                },[])
            }

Here is the Error i'm getting

                    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>)

This is the Desired Result i'm trying to get

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

1 Answers1

1

The "reducer" callback of Array#reduce(reducer, initValue, ...) has to return the accumulator value (return acc;) to use in the next round.
This is because you might have to handle primitive values that aren't staying consistent references e.g. like in situation you want to sum a list of numbers: [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);
Niklas E.
  • 1,848
  • 4
  • 13
  • 25
  • Thanks. I've tested the solution. I've noticed that the acculator returns something like this DEPOSIT: 0.554933, WITHDRAWAL: 0.761543 . How do i group it to each group name like this ETH { DEPOSIT: 0.554933, WITHDRAWAL: 0.761543 }? – Kinyua James Apr 18 '22 at 10:48
  • 1
    @KinyuaJames I added the variable "groupedBalance". You can see it's value under "FINAL RESULTS" if you run the code snipped and scroll down in the console outputs. – Niklas E. Apr 18 '22 at 10:51
  • Thank you very much. I've spent sleepless nights to solve this. – Kinyua James Apr 18 '22 at 10:56
  • 1
    @KinyuaJames You will run into this problem: https://stackoverflow.com/questions/1458633 You might have to do some rounding. – Niklas E. Apr 18 '22 at 10:57
  • @KinyuaJames I also added a `{DEPOSIT: 0, WITHDRAWAL: 0}` acc value and use the add-to operator `+=` to actually sum the values, so that you not just subtracting the last deposit and withdrawal. – Niklas E. Apr 18 '22 at 11:00