为了账号安全,请及时绑定邮箱和手机立即绑定

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

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

摇曳的蔷薇 2018-12-21 19:14:24
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}]}]
查看完整描述

1 回答

  • 1 回答
  • 0 关注
  • 963 浏览
慕课专栏
更多

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信