如何向JSON对象添加元素

时间:2016-08-31 09:58:08

标签: javascript json

我一直在努力解决这个问题6个小时,而我仍然无法修复它。

也许你可以帮我解决这个问题。

我有一个js对象:

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

         };

我需要在结果中得到这样的东西:

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"
        }
        ]
    }
    ],



};

我需要动态生成这个对象。

我这样做了:

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

之后我做了JSON.stringify(year_data)并且在我将19更改为变量名之前它已经工作了。

(控制台)之前:{"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":[]}

更改后

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

我明白了:

{"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":[]}

而且我不知道如何解决它。

我试过这个 Using a variable for a key in a JavaScript object literal

但结果我得到了:

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":[]}

所以,我需要这两种变体

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

或 解决这个问题 year_data.jan[19]=[{1:"Some text for 19_1", 2:"Some text for 19_2"}];var json=JSON.stringify(year_data);

并得到

{"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":[]}

而不是

{"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":[]}

伙计们,我真的需要你的帮助

2 个答案:

答案 0 :(得分:1)

在版本ES6之前,Javascript不支持对象初始值设定项中的动态属性,因此您必须这样做:

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

在ES6中,动态属性用方括号括起来,例如:

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

答案 1 :(得分:1)

sass / scss