I am facing a challenge in looping through an array that contains another array as one of the parameters. My goal is to iterate through this nested array according to specific requirements, and then execute a function once the parent loop is finished. Can anyone help me identify where I might be going wrong with this task?
const schedule = {}
data.schedule.forEach(item => {
let date = moment(item.date).format('YYYY-MM-DD')
let eventList = []
item.events.forEach(event => {
let start = moment(event.start).format('h:mm A')
let end = moment(event.end).format('h:mm A')
let time = `${start} - ${end}`
eventList.push({time: time, name: event.name})
})
return Promise.all(eventList).then(list => {
console.log('list', list)
schedule[`${date}`] = list
})
})
// my current issue:
Promise.all(schedule).then(list => {
console.log('schedule:', list)
})
// but this throws an error:
// TypeError: (var)[Symbol.iterator] is not a function
// at Function.all (native)
The desired output should resemble the following object:
{'2017-12-06': [
{time: '9am - 10am', name: 'Jackson Home'},
{time: '11AM - 3PM', name: 'Jackson Home'},
{time: '3PM - 8PM', name: 'Jackson Home'}
]}