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

0

Currently I have an array object 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 Underscore.js to group this array, method 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 for grouping using only 1 JSON attribute, I was wondering if there is how to group by more than 1 attribute using Underscore. If there are alternatives to underscore for this case please let me know.

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

    
asked by anonymous 28.11.2016 / 13:59

1 answer

0

The solution is to group together to create a comparison combination in my case I needed idProduto and 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;
  });
    
29.11.2016 / 12:06