Javascript - Dynamic object keys (second key) -


i can create dynamic object follows:

var year=2103; var month=9;  var selected={}; selected[year][month]=true; 

but following gives undefined object error presume because first key has not been created , javascript not automatically doing that.

selected[year]=true; 

but following gives undefined object error presume because first key has not been created , javascript not automatically doing that.

yes, have create it:

var year = 2103; var month = 9; var selected = {}; selected[year] = {}; selected[year][month] = true; 

if you're unsure if object exists, , not want overwrite it:

selected[year] = selected[year] || {}; 

as shortcut populate object if missing & assign month key in 1 step:

(selected[year]||(selected[year]={}))[month] = true; 

Comments

Popular posts from this blog

html - How to style widget with post count different than without post count -

How to remove text and logo OR add Overflow on Android ActionBar using AppCompat on API 8? -

javascript - storing input from prompt in array and displaying the array -