Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

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

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'] 
    ],
  ]
]
about 4 years ago · Juan Pablo Isaza
1 Respuestas
Responde la pregunta

0

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

about 4 years ago · Juan Pablo Isaza Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda