javascript 两个循环数组比对 后 push 对象问题

hours, times两个数组比对,后生成一个新的数组。

希望得到:

[{"date":"2017-08-30","hours":[{"hour":"08:00","state":true},{"hour":"08:30"}]},{"date":"2017-08-31","hours":[{"hour":"08:00"},{"hour":"08:30","state":true}]}]


//目前代码

var hours = [{"hour":"08:00"},{"hour":"08:30"}];


var time=[{ "date":"2017-08-30","time":"08:00"},

{"date":"2017-08-31","time":"08:30"}]


var newdata=[]


times.forEach(function(e,i){

    hours.forEach(function(a,j){

      if(e.date==a.hour){

        a.state=true

      }

    })

    newdata.push({

      date:e.date,

      hours:hours

    })

})

console.log(JSON.stringify(newdata))


//输出结果

[{"date":"2017-08-30","hours":[{"hour":"08:00","state":true},{"hour":"08:30","state":true}]},{"date":"2017-08-31","hours":[{"hour":"08:00","state":true},{"hour":"08:30","state":true}]}]


摇曳的蔷薇
浏览 964回答 1
1回答
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

JavaScript