Search code examples
javascriptbixby

Pass in structure correctly (js)


Face some problem in passing data into structure correctly

This is the structure

enter image description here

And this is how i pass in

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

}

And error pop out

enter image description here

Where did i gone wrong ,

And for extra question, is my if-else condition for currentTimeHour wrote correctly?

Regards.


Solution

  • forEach is defined on arrays where as optionList.option is not an array.

    Try using only optionList

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