Group object in javascript by categories

Asked

Viewed 86 times

-1

I have a code with Datatable and wanted to group and add values in situations

  var table = $('#tabelaDadosFinanc').DataTable();

    var data = table
    .rows()
    .data();

    var total = 0;


    for (var i = 0; i < data.length; i++){

        var valor = data[i]['valor'];
        var moeda = data[i]['moeda'];

        removeFirst = valor.replace('<span class="text-danger" id="negativo">','');

            valores = removeFirst.replace('</span>','');

            a = valores.replace('.','');
            b = a.replace(',', '.')

            total +=parseFloat(b);

    }
     console.log(total);

You would need to Group by currency type and add up the values,

ex: as I have today

Moeda |  Valor
Real  | -56,12
Real  | -48,54
AU    | -46,60
Real  | -46,60
Real  | -46,60
Real  | 46,60
Real  | 48,54
Real  | 56,12 

to look like this at the end

 Real| -46,60
 AU | -46,60
  • You will consider currency and name?

  • consider first the currency - and group the values belonging to the currency ex: Real -> -46,60 AU -> -46,60

  • The values at the end of the grouping are not those for example Real | will give another value?

  • the value of the grouping is the sum of the values, total of real, total of Au

  • But Real is different from R$ which, in turn, is different from RS. The doubt is, Real, R$ and RS are the same thing?

  • Yes they are the same things

  • Felipe, just a doubt how to convert this (var value = date[i]['value']; var currency = date[i]['currency'];) to const date = [ {"currency": "Real", "value":-56.12}, {"currency": "Real", "value":-48.54}, {"currency": "AU", "value":-46.60}, {"currency": "Real", "value":-46.60}, {"currency": "Real", "value":-46.60}, {"currency": "Real", "value":46.60}, {"currency": "Real", "value":":48.54}, {"currency": "Real", "value":56.12 } ];

Show 2 more comments

1 answer

0


To do this, just do the following:

const data = [
  {"moeda": "Real", "valor":-56.12},
  {"moeda": "Real", "valor":-48.54},
  {"moeda": "AU", "valor":-46.60},
  {"moeda": "Real", "valor":-46.60},
  {"moeda": "Real", "valor":-46.60},
  {"moeda": "Real", "valor":46.60},
  {"moeda": "Real", "valor":48.54},
  {"moeda": "Real", "valor":56.12 }
];

const resultado = {};

data.forEach(item => {
  if(resultado.hasOwnProperty(item.moeda)) {
    resultado[item.moeda] += item.valor;
  } else {
    resultado[item.moeda] = item.valor;
  }
});

console.log(resultado);

console.log("MOEDA\t| VALOR");
for(var moeda in resultado) {
  console.log(moeda+"\t| "+resultado[moeda]);
}

If you want to make a currency translation, you can do it as follows:

const siglaMoedas = {
  "R$": ["Real","RS"],
  "AU$": ["AU", "AUD"],
  "US$": ["Dolar", "USD"]
};

function pegaSigla(nomeMoeda) {
  for(let sigla in siglaMoedas) {
    if(siglaMoedas[sigla].includes(nomeMoeda)) return sigla;
  }
  return nomeMoeda;
}

console.log(pegaSigla('Real'));
console.log(pegaSigla('AU'));
console.log(pegaSigla('USD'));
console.log(pegaSigla('Dolar Canadense'));

And use the second script to replace the parts you have item.moeda for pegaSigla(item.moeda)

Browser other questions tagged

You are not signed in. Login or sign up in order to post.