正确传入结构(js)

Pass in structure correctly (js)

在将数据正确传递到结构中时遇到一些问题

这是结构

这就是我传递的方式

var dates = require('dates');

module.exports.function = function getStart() {
  var optionList = [
  {option : "Latest headlines"},
  {option : "Latest news"},
  {option : "Top headlines"},
  {option : "Top news"}
]
  var currentTimeHour = dates.ZonedDateTime.getHour
  var timePeriod = "";

  if (currentTimeHour == 0 && currentTimeHour <= 12) {
    timePeriod = "M"//Morning
  } else if (currentTimeHour >= 13 && currentTimeHour <= 20) {
    timePeriod = "A"//Afternoon
  } else if (currentTimeHour <=23){
    timePeriod = "N"//Night
  }else {
    timePeriod = null
  }

  var menu = {};

  optionList.option.forEach(function(value,index,array){
    menu[index] = {
      whatuserwant : optionList[index],
      timePeriod : timePeriod
    }

  });

  return menu

}

然后错误弹出

我哪里错了,

还有一个额外的问题,我的 currentTimeHour 的 if-else 条件写对了吗?

此致。

forEach 是在数组上定义的,其中 optionList.option 不是数组。

尝试只使用 optionList

optionList.forEach(function(value,index,array){
        menu[index] = {
          whatuserwant : value.option,
          timePeriod : timePeriod
        }
      });