arrays - Javascript object sum value based on properties effeicently -


i have 3 objects inside array in js below

[{"2013-03-02t00:00": 300}, {"2013-03-01t00:00": 200},{"2013-03-02t00:00": 50}] 

i want below output above array.

[{"2013-03-02t00:00": 350} , {"2013-03-01t00:00": 200}] 

it can done looping through , adding, there efficient way can it?

thanks in advance.

var mylist = [{"2013-03-02t00:00": 300}, {"2013-03-01t00:00": 200},{"2013-03-02t00:00": 50}]; var result = {}; var item = null, key = null; for(c=0; c<mylist.length; c++) {    item=mylist[c];    key = object.keys(item)[0];    item=item[key];     if(!result[key]) result[key] = item;    else result[key] += item; }  console.log(result); 

i leave exercise reader put result requested form. (after should solve @ least part of problem :)


Comments

Popular posts from this blog

blackberry 10 - how to add multiple markers on the google map just by url? -

php - guestbook returning database data to flash -

delphi - Dynamic file type icon -