我想匹配时间和日期 结果应该是每个日期的时间总和...... 例如...... 06/27 - 应该是30和90的总和......等等。 我怎么能这样做......
var array_date=["2017/06/27","2017/06/26","2017/06/27","2017/06/26","2017/06/28"]
var array_time=["30","50","90","120","20"]
var obj = array_date;
for (var i = 0 in obj) {
console.log("rr:"+obj[i]);
}
//To filter date I used below method...anyway.
var cleanArray = displayed_date.filter((value,index,self)=>{ return (self.indexOf(value) === index )});
答案 0 :(得分:0)
首先,由于您正在添加,最好将字符串转换为数字:
array_time = array_time.map(t=> +t);
接下来,按日期对时间进行分组和总结:
let data = array_date.reduce((acc, d, i)=> {
if(acc.hasOwnProperty(d)) acc[d] += array_time[i];
else acc[d] = array_time[i];
return acc;
}, {});
答案 1 :(得分:0)
你可以这样做:
<script>
array_date=["2017/06/27","2017/06/26","2017/06/27","2017/06/26","2017/06/28"];
array_time=["30","50","90","120","20"];
var oData = [];
for(var i = 0; i < array_date.length; i++ ){
if(Object.keys(oData).length > 0){
var icount = 0;
for(var x = 0; x < Object.keys(oData).length; x++){
if(oData[x]['date'] == array_date[i]){
oData[x]['sum'] = parseInt(oData[x]['sum']) + parseInt(array_time[i]);
icount++;
}
}
if(icount == 0){
var oCreate = {"date":array_date[i], "sum":parseInt(array_time[i])}
oData.push(oCreate);
}
}else{
var oCreate = {"date":array_date[i], "sum":parseInt(array_time[i])}
oData.push(oCreate);
}
}
console.log(JSON.stringify(oData)); //oData is the final variable answer here.
</script>
希望它会有所帮助。