I encountered a JSON object like this:
var Events = {
"Jan" : [],
"Feb" : [],
"Mar" : {
"23" : [
{"eventName" : "Start", "eventYear" : 2015, "eventDescription" : "First event description"},
{"eventName" : "Start 2", "eventYear" : 2016, "eventDescription" : "Second event description"},
{"eventName" : "Start 3", "eventYear" : 2017, "eventDescription" : "Third event description"}
],
"24" : [
{"eventName" : "Start", "eventYear" : 2015, "eventDescription" : "First event description for 24"},
{"eventName" : "Start 2", "eventYear" : 2016, "eventDescription" : "Second event description for 24"}
]
},
"Apr" : [],
"May" : [],
"Jun" : [],
"Jul" : [],
"Aug" : [],
"Sep" : [],
"Oct" : [],
"Nov" : [],
"Dec" : []
}
I'm attempting to extract the events array for the current month:
window.addEventListener('DOMContentLoaded', init);
var date, month_names_short, thisMonthEvents;
function init(){
date = new Date();
month_names_short = ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec'];
thisMonthEvents = Events.month_names_short[date.getMonth()];
};
However, I'm encountering an error message
TypeError: Cannot read property '2' of undefined
on this line of code:
thisMonthEvents = Events.month_names_short[date.getMonth()];
Could someone please point out what mistake I am making and provide a solution? Thank you.