Bhanu
Bhanu

Reputation: 419

Build JSON with sub levels

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

Answers (2)

Mister Jojo
Mister Jojo

Reputation: 22265

different way, still JS object

var a = { };
a['nameInfo'] = { name: 'Bhanu' };

console.log(a);

Upvotes: 0

None of your Beez Wax
None of your Beez Wax

Reputation: 1333

Easiest way is to do the following:

let a = {
  nameInfo: {
    name: 'Bhanu'
  }
}

Upvotes: 1

Related Questions