i have following 2 arrays in javascript:
"total":[[1370923200000,"66"],[1371009600000,"42"],[1371096000000,"23"]] "successful":[[1370923200000,"5"],[1371096000000,"2"],[1371182400000,"0"]]
i'd combine them 1 array / object looks this:
{date:1370923200000, total:"66", successful:"5"}, {date:1371009600000, total:"42"}, {date:1371096000000, total:"23", successful:"2"}, {date:1371182400000, successful:"0"}
i've tried multiple different solutions, looping through both arrays, can't seem figure out elegant solution.
here have:
var total = [[1370923200000, "66"],[1371009600000, "42"],[1371096000000, "23"]]; var successful = [[1370923200000, "5"],[1371096000000, "2"],[1371182400000, "0"]]; var combined = {}; for(var i=0; i<total.length; i++){ combined[total[i][0]] = {date: total[i][0], total: total[i][1]}; } for(var i=0; i<successful.length; i++){ if(successful[i][0] in combined){ combined[successful[i][0]].successful = successful[i][1]; } else{ combined[successful[i][0]] = { date: successful[i][0], successful: successful[i][1] }; } } var result = []; for(var key in combined){ result.push(combined[key]); } alert(result.tosource());
and working fiddle http://jsfiddle.net/erjez/
Comments
Post a Comment