1

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

Community
  • 1
  • 1
JackSw_13
  • 13
  • 3
  • use `[current_day]` as property name to pass variable – Maxx Aug 31 '16 at 10:00
  • Why do you want to have a object inside a day that starts of from 1 and not 0? really impractical - i suggest to use an array instead – Endless Aug 31 '16 at 10:03
  • Possible duplicate of [Is there a way that use var to create JSON objects with a key?](http://stackoverflow.com/questions/882727/is-there-a-way-that-use-var-to-create-json-objects-with-a-key) – Herr Derb Aug 31 '16 at 10:05

2 Answers2

1

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
});
georg
  • 211,518
  • 52
  • 313
  • 390
1
var current_day=19;
var obj = {}; 
year_data.jan.push(
    obj[current_day]: [{1:"Some text for 19_1",
          2:"Some text for 19_2"}]

);