在正确地将数据传递到结构中时遇到一些问题
这是结构
这就是我进入的方式
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
}
});
关于javascript - 正确传入结构(js),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60864592/