Ich habe 6 Stunden lang mit diesem Problem zu kämpfen und kann es immer noch nicht beheben.So fügen Sie Elemente zum JSON-Objekt hinzu
Vielleicht können Sie mir dabei helfen.
Ich habe ein js Objekt:
var year_data = {
"jan": [],
"feb": [],
"mar": [],
"apr": [],
"may": [],
"jun": [],
"jul": [],
"aug": [],
"sep": [],
"oct": [],
"nov": [],
"dec": [],
};
und ich brauche so etwas wie dies in Folge zu erhalten:
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"
}
]
}
],
};
Ich muss dynamisch dieses Objekt erzeugen.
Ich habe diese:
year_data.jan.push(
{19: [{1:"Some text for 19_1",
2:"Some text for 19_2"}]
}
);
Danach habe ich JSON.stringify tat (year_data) Und es hat funktioniert hatte, bevor ich 19 Namen Variable geändert.
(Konsole) Vorher: {"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":[]}
Nach
var current_day=19;
year_data.jan.push(
{current_day: [{1:"Some text for 19_1",
2:"Some text for 19_2"}]
}
);
Wechsel habe ich dieses:
{"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":[]}
Und ich weiß nicht, wie es zu beheben.
Ich habe schon versucht, diese Using a variable for a key in a JavaScript object literal
Aber in Folge bekam ich:
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":[]}
Also, ich brauche entweder Variante, dies zu tun
year_data.jan.push({**VARIABLE_name**: [{1:"Some text for 19_1", 2:"Some text for 19_2"}] });
oder etwas zur Behebung dieses Problems year_data.jan[19]=[{1:"Some text for 19_1", 2:"Some text for 19_2"}];var json=JSON.stringify(year_data);
und bekam
{"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":[]}
statt
{"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":[]}
Leute, ich brauche wirklich deine Hilfe
Verwendung '[current_day]' als Eigenschaftsnamen Variable übergeben – Maxx
Warum Sie haben wollen ein Objekt innerhalb eines Tages, das von 1 und nicht 0 beginnt?wirklich unpraktisch - ich schlage vor, stattdessen ein Array zu verwenden – Endless
Mögliches Duplikat von [Gibt es eine Möglichkeit, die var verwendet, um JSON-Objekte mit einem Schlüssel zu erstellen?] (http://stackoverflow.com/questions/882727/is-therea- way-that-use-var-Erstelle-json-objects-with-a-key) –