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
.
Is that what you’re trying to do? http://answall.com/questions/15064/como-agregar-propriedades-de-objetos-javascript/15074#15074
– Lucas Lima
"(...)need a function(...)" What you have tried already?
– Oralista de Sistemas
but with the sum of several indexes, by date, would have an example?
– Jhonatan
Renan, I have only the function that returns, all these arrays, now to group, do not know how to do
– Jhonatan
@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 ;)
– Oralista de Sistemas
What are the values you want to add up? can you at least give an example of the final format you want?
– Sergio
Renan, I talked about the function, because php has the function q makes it easy, Sergio, the final format is array msm
– Jhonatan
Only improve the issue that it is very good, lack to improve and put an example of output!
– user6026
@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...
– Sergio