Reputation: 419
I am trying to build JSON as below.
{
"nameInfo": {
"name": "Bhanu"
}
}
I tried below code and its working fine.
a = {}
a.nameInfo = {}
a.nameInfo.name="John"
console.log(a)
However, just checking if there is a better way to do the same thing. Do I always have to initialize a.nameInfo to empty object before adding a property under nameInfo?
Upvotes: 0
Views: 179
Reputation: 22265
different way, still JS object
var a = { };
a['nameInfo'] = { name: 'Bhanu' };
console.log(a);
Upvotes: 0
Reputation: 1333
Easiest way is to do the following:
let a = {
nameInfo: {
name: 'Bhanu'
}
}
Upvotes: 1