JackSw_13 JackSw_13 - 3 months ago 9
JSON Question

How to add elements to JSON Object

I have been struggling with this problem for 6 hours and I'm still not able to fix it.

Maybe you can help me with this.

I have a js object:

var year_data = {
"jan": [],
"feb": [],
"mar": [],
"apr": [],
"may": [],
"jun": [],
"jul": [],
"aug": [],
"sep": [],
"oct": [],
"nov": [],
"dec": [],

};


and I need to get something like this in result:

var year_data= {

jan : [
{
23 : [
{
"1" : "some text for 23",
"2" : "some text for 23_1",
"3" : "some text for 23_2"
}
],

26 : [
{
"1" : "some text for 26",
"2" : "some text for 26_1",
"3" : "some text for 26_2"
}
]
}
],



feb : [
{
17 : [
{
"1" : "some text for 17_1",
"2" : "some text for 17_2",
"3" : "some text for 17_3"
}
]
}
],



};


I need to generate this object dynamically.

I did this:

year_data.jan.push(
{19: [{1:"Some text for 19_1",
2:"Some text for 19_2"}]
}
);


After that I did JSON.stringify(year_data) And it had worked before I Changed 19 to variable name.

(console) Before:
{"jan":[{"19":[{"1":"Some text for 19_1","2":"Some text for 19_2"}]}],"feb":[],"mar":[],"apr":[],"may":[],"jun":[],"jul":[],"aug":[],"sep":[],"oct":[],"nov":[],"dec":[]}


After changing

var current_day=19;
year_data.jan.push(
{current_day: [{1:"Some text for 19_1",
2:"Some text for 19_2"}]
}
);


I got this:

{"jan":[{"current_day":[{"1":"Some text for 19_1","2":"Some text for 19_2"}]}],"feb":[],"mar":[],"apr":[],"may":[],"jun":[],"jul":[],"aug":[],"sep":[],"oct":[],"nov":[],"dec":[]}


And I don't know how to fix it.

I've tried this
Using a variable for a key in a JavaScript object literal

But in result I got:

year_data.jan[19]=[{1:"Some text for 19_1", 2:"Some text for 19_2"}];
var json=JSON.stringify(year_data);

console: {"jan":[null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,[{"1":"Some text for 19_1","2":"Some text for 19_2"}]],"feb":[],"mar":[],"apr":[],"may":[],"jun":[],"jul":[],"aug":[],"sep":[],"oct":[],"nov":[],"dec":[]}





So, I need either variant for doing this

year_data.jan.push({**VARIABLE_name**: [{1:"Some text for 19_1", 2:"Some text for 19_2"}] });


or
something for fix this
year_data.jan[19]=[{1:"Some text for 19_1", 2:"Some text for 19_2"}];var json=JSON.stringify(year_data);


AND got

{"jan":[{"19":[{"1":"Some text for 19_1","2":"Some text for 19_2"}]}],"feb":[],"mar":[],"apr":[],"may":[],"jun":[],"jul":[],"aug":[],"sep":[],"oct":[],"nov":[],"dec":[]}


instead of

{"jan":[null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,[{"1":"Some text for 19_1","2":"Some text for 19_2"}]],"feb":[],"mar":[],"apr":[],"may":[],"jun":[],"jul":[],"aug":[],"sep":[],"oct":[],"nov":[],"dec":[]}


Guys, I really need your help

Answer

Prior to version ES6, Javascript didn't support dynamic properties in object initializers, so you have to do it like this:

var current_day = 19;
var temp = {};
temp[current_day] = whatever;
year_data.jan.push(temp);

In ES6, a dynamic property is enclosed in square brackets, for example:

year_data.jan.push({
   [current_day]: whatever
});