Underscore JS, Group object array by more than 1 attribute contained in an object

Asked

Viewed 326 times

0

Hello I currently have an object of arrays that contains the following structure

{
  "ID_Local_leitura": "2134236478dysa743242579f",
  "idO": "11342s36478dysa743242579",
  "timeStamp": "2016-11-11T02:00:00.000Z",
  "quantidadeAnimais": 50,
  "tipoDeAnimal": "Corte ",
  "quantidadeProduto": "Nível 0",
  "sacosProduto": 1,
  "idProduto": "11342136448dyty43242579",
  "loteProduto": "1513/16G",
  "imagem": "/imgs/foto1432543583123.jpeg",
  "categoria": "Categoria 3",
  "resp": "leitura",
  "_id": "33065027c0a359d395238fd76787b3fc",
  "_rev": "2-33065027c0a359d395238fd76787b3fc",
  "peso": 30,
  "resultado": 0.6
}

I’m currently using the Underscore.js to group this array, here below.

function filtro(filtrar) {
    var notNull = _.negate(_.isNull);
    var groups = _.groupBy(relatorioData.leituras, function(note){
      return _.find(_.pick(note, filtrar), notNull);
    });

The above method works perfectly to group using only 1 JSON attribute, I was wondering if there is how to group by more than 1 attribute using Underscore. If there are any lands on the underscore for this case please tell me.

For example I want to group everything that has ID_Local_leitura,timeStamp,categoria and idProduto as the same value in a new object array, in my searches I found only the ordering by Mutiplos values using underscore.

1 answer

0


The solution consists of the time to group create a comparison combination in my case needed to idProduto and of timeStamp

var groups = _.groupBy(relatorioData.leituras, function(value){
    //Aqui digo que o index retornado de ver o id do produto + a data
    return value.idProduto + '#' + value.timeStamp;
  });

Browser other questions tagged

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