Grouping and adding array in JavaScript

-2

How to make a function to only add values that are of the same date? I'm passing an array of arrays:

[{ servicos: 0, remessa: 503, materiais: 0 , retorno: 598, entrada: 0, date: new Date("2011/12/20") },
{ servicos: 302, remessa: 0, materiais: 412 , retorno: 234, entrada: 864, date: new Date("2011/12/20") },
{ servicos: 276, remessa: 211, materiais: 0, retorno: 598, entrada: 864, date: new Date("2011/13/20") },
{ servicos: 543, remessa: 5033, materiais: 412 , retorno: 0, entrada: 864, date: new Date("2011/13/20") },
{ servicos: 876, remessa: 503, materiais: 412 , retorno: 0, entrada: 0, date: new Date("2011/13/20") }]

In the above example, how do I return an array from day 12 and another from day 13?

    
asked by anonymous 15.05.2014 / 16:20

2 answers

3

I imagine what you want to do is this:

var arr = [{ servicos: 0, remessa: 503, materiais: 0 , retorno: 598, entrada: 0, date: new Date("2011/12/20") },
           { servicos: 302, remessa: 0, materiais: 412 , retorno: 234, entrada: 864, date: new Date("2011/12/20") },
           { servicos: 276, remessa: 211, materiais: 0, retorno: 598, entrada: 864, date: new Date("2011/13/20") },
           { servicos: 543, remessa: 5033, materiais: 412 , retorno: 0, entrada: 864, date: new Date("2011/13/20") },
           { servicos: 876, remessa: 503, materiais: 412 , retorno: 0, entrada: 0, date: new Date("2011/13/20") }];

var result = new Object();
arr.filter(function (i){ 
  if(result.hasOwnProperty(i.date)){
    result[i.date].servicos += i.servicos;
    result[i.date].remessa += i.remessa;
    result[i.date].materiais += i.materiais;
    result[i.date].retorno += i.retorno;
    result[i.date].entrada += i.entrada;
  }else{
    result[i.date] = new Object();
    result[i.date].servicos = i.servicos;
    result[i.date].remessa = i.remessa;
    result[i.date].materiais = i.materiais;
    result[i.date].retorno = i.retorno;
    result[i.date].entrada = i.entrada;
  }
});

This will return you a Object whose key is the date. For each date it saves the data of each entry of the object.

If you really need to use a vector, you can do the following:

var arr = [{ servicos: 0, remessa: 503, materiais: 0 , retorno: 598, entrada: 0, date: new Date("2011/12/20") },
           { servicos: 302, remessa: 0, materiais: 412 , retorno: 234, entrada: 864, date: new Date("2011/12/20") },
           { servicos: 276, remessa: 211, materiais: 0, retorno: 598, entrada: 864, date: new Date("2011/13/20") },
           { servicos: 543, remessa: 5033, materiais: 412 , retorno: 0, entrada: 864, date: new Date("2011/13/20") },
           { servicos: 876, remessa: 503, materiais: 412 , retorno: 0, entrada: 0, date: new Date("2011/13/20") }];

var result = [];
arr.filter(function (i){ 
  var hasElement = false;
  var j;
  for(j = 0; j < result.length; j++){
    if(result[j].date.getTime() === i.date.getTime()){
      hasElement = true;
      break;
    }
  }
  if(hasElement){
    result[j].servicos += i.servicos;
    result[j].remessa += i.remessa;
    result[j].materiais += i.materiais;
    result[j].retorno += i.retorno;
    result[j].entrada += i.entrada;
  }else{
    result.push(i);
  }
});

With this entry, this filter will return a Array with two elements in the result variable.

    
15.05.2014 / 17:28
1

If you have more than one array you can group them using the following form:

var first_json = {"name":"joe", "age":27};
var second_json = {"name":"james", "age":32};

var jsons = new Array();
jsons.push(first_json);
jsons.push(second_json);

Getting the Result:

jsons = [
    {"name":"joe", "age":27},
    {"name":"james", "age":32}
]

In the case of the filter you can do this:

var data = { "items":[ { "id": 1, "category": "cat1" }, {
"id": 2, "category": "cat2" }, { "id": 3, "category": "cat1" } ]};

var returnedData = $.grep(data.items, function(element, index){
      return element.id == 1;
});
// console.log(returnedData);

alert(returnedData[0].id + "  " + returnedData[0].category);
    
15.05.2014 / 16:27