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
Post a Comment