I need to create a nested object that looks like this
var a = "example"
{Chatting : {a :{ 'id' :a}}}
I can't really find anything online, I tried the code below and its not working
myobj['Chatting'][a] = {
'id': a
}
I need to create a nested object that looks like this
var a = "example"
{Chatting : {a :{ 'id' :a}}}
I can't really find anything online, I tried the code below and its not working
myobj['Chatting'][a] = {
'id': a
}
At once:
var a = "example"
var obj = {Chatting : {a :{ 'id' :a}}};
console.log(obj);
Step by step:
var a = "example"
var obj = {}; // create the object obj
obj.Chatting = {}; // create the sub-object Chatting of obj
obj.Chatting.a = {}; // create the sub-object a of Chatting
obj.Chatting.a.id = a; // set it's id to a
console.log(obj);
Your question is quite unclear. But i guess, you want get element a, right?
let a = {Chatting : {a :{ 'id' : 'a'}}};
console.log(a.Chatting.a);
var a = "example";
var obj = {Chatting : {a :{ 'id' :'a'}}};
Please note that in an object {key:value} key is a string not variable. I am defining here id:'a', which will output
console.log(obj['Chatting']['a']['id']); // 'a'
and adding variable to the object
obj['Chatting']['a'] = {'id':a};
which will output
console.log(obj['Chatting']['a']['id']); // 'example'