Rashmi Vishwakarma Rashmi Vishwakarma - 1 year ago 65
Javascript Question

How to access key from nested object in javascript

Can you please help me how can I access "name" key from obj3. Please find below example.

I am looking for good approch, I dont want to do :

obj.obj1.obj2.obj3.name

var obj = {
obj1 : {
obj2: {
obj3: {
name: 'jhon'
}
}
}
}


Thanks!

Answer Source

You could theoretically destructure using es6 for example

const {obj1: {obj2: { obj3: {name: b}}}} = obj console.log(b) //jhon

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download