I am using the firebase realtime database to get data into my app. I am using json to sort my data and I try to get a variable as my value as shown in the code. I tried to use JSON.stringify(favoriteMenu)
and "{favoriteMenu}"
but that does not work. Does somebody has an idea? Thank you.
var favoriteMenu = "Pizza";
{
"Menu" : {
"09:09:2020" : {
"Menu1" : favoriteMenu
}
}
}
I am new in json but the following is what I sued to get the results from the added image. But when I tried it with a variable it gives me an error when I want to import the json file to the realtime database from firebase.
{
"Menu" : {
"09:09:2020" : {
"Menu1" : "Coconut Fish Curry"
}
}
}
If your question is how to set the key Menu1
of the object at key 09:09:2020
which is inside object at key Menu
to the value of the variable named favoriteMenu
then:
var favoriteMenu = "Pizza";
var o = {
"Menu" : {
"09:09:2020" : {
"Menu1" : favoriteMenu
}
}
};
o.Menu["09:09:2020"].Menu1 = favoriteMenu;
console.log(o.Menu["09:09:2020"].Menu1 === favoriteMenu);
Most likely you were confused about how to index object with keys like 09:09:2020
, as you can see it is possible by using square brackets and wrapping key as a string.