Group and add array in Javascript

Asked

Viewed 3,773 times

-2

How to do a function to sum only values that are of the same date?
I 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 to return an array from day 12 and another from day 13?

  • 2

    Is that what you’re trying to do? http://answall.com/questions/15064/como-agregar-propriedades-de-objetos-javascript/15074#15074

  • "(...)need a function(...)" What you have tried already?

  • but with the sum of several indexes, by date, would have an example?

  • Renan, I have only the function that returns, all these arrays, now to group, do not know how to do

  • 1

    @Jhonatan So here’s a hint: instead of "I need a function...", say "how do I..." The first sentence may give the impression that you don’t want to learn and only that someone solves your problem for you. The second gives the impression that you are striving for the solution. So people are more willing to help you ;)

  • What are the values you want to add up? can you at least give an example of the final format you want?

  • Renan, I talked about the function, because php has the function q makes it easy, Sergio, the final format is array msm

  • 1

    Only improve the issue that it is very good, lack to improve and put an example of output!

  • @Jhonatan can give an example? I still don’t know what you want. What values should be added with which ones? This is very simple to do but I will answer only when I understand what you want...

Show 4 more comments

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 one Object whose key is the date. For each date it saves the data of each object entry.


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 entrance, this filter will return a Array with two elements in the variable result.

  • Dude, that’s almost it, I’d just like to change it so that instead of the object having the date name, it’s an array with "date field:"

  • Do you have any specific reason? Because, otherwise, it is less expensive this solution. If you want to access by date just make a for, as follows for( k in result, that it will return all dates that have information.

  • is that I need to use on Kendo’s heart ui, and there accept [{},{},{}]

  • In that case, I’ll add an alternate form to the answer.

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 way:

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

Browser other questions tagged

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