简体   繁体   English

Javascript - 对象:如何访问嵌套对象?

[英]Javascript - Objects: How To Access Nested Objects?

I went through every single question from SO, as well as Google but to no avail, this is why I'm creating a duplicate because clearly, my case is different since none of the answers work.我检查了 SO 和 Google 的每一个问题,但无济于事,这就是我创建副本的原因,因为很明显,我的情况不同,因为没有一个答案有效。

First of all, I don't understand why I can't just simply access an object within an object simply by using the .首先,我不明白为什么我不能简单地通过使用. notation as such: myObject.level1.level2.level3whichIsAObject , it returns undefined .符号如下: myObject.level1.level2.level3whichIsAObject ,它返回undefined My structure is:我的结构是:

myMainOjbect = { 
    attachment: {
        handle: 'attachment',
        slug: 'Images',
        state: {
            success: true,
            data: {
                activated: false,
                info: {
                    demo: 'demo-1',
                    component: 'attachment',
                },
                success: true,
            }
        }
    }
},
..

First, I loop through the high-level:首先,我遍历高级:

for(let key in myMainOjbect) {
    const objectData = objectData[key];
}

Which shows me everything that's inside the attachment slug.这向我展示了attachment中的所有内容。 But if I want to get objectData.state , suddenly, it's returned as undefiend .但是如果我想得到objectData.state ,突然间,它被返回为undefiend

What's going on here?这里发生了什么? Why can't I just simply access things and second, how can I actually do it?为什么我不能简单地访问东西,其次,我该怎么做呢?

The weirdest part is if I store my original object as a global in the console and I do the same thing I did in my code:最奇怪的部分是,如果我将原始 object 作为全局存储在控制台中,并且我在代码中执行相同的操作:

在此处输入图像描述 ...it works. ...有用。

Here's the error I'm (not) getting:这是我(没有)得到的错误:

 const myObject = { attachment: { handle: 'attachment', slug: 'Images', state: { success: true, data: { activated: false, info: { demo: 'demo-1', component: 'attachment', }, success: true, } } }, widgets: { handle: 'widgets', slug: 'Widgets', state: { success: true, data: { activated: false, info: { demo: 'demo-1', component: 'widgets', }, success: true, } } }, }; for (const key in myObject) { console.log(myObject[key].state); }

In this demo, it works, except on my code, which is 1:1 copy...it doesn't.在这个演示中,它可以工作,除了我的代码,它是 1:1 副本......它没有。

You're accessing the variable which is not coming from the loop.您正在访问不是来自循环的变量。

You need to do it like this.你需要这样做。

 const myMainOjbect = { attachment: { handle: 'attachment', slug: 'Images', state: { success: true, data: { activated: false, info: { demo: 'demo-1', component: 'attachment', }, success: true, } } } }; for (const key in myMainOjbect) { console.log(myMainOjbect[key].state) }

Try this code:试试这个代码:

for (let key in myMainOjbect) {
    const objectData = myMainOjbect[key];
    console.log(objectData.state)
}

const objectData = objectData[key]; is not the same as console.log(myMainOjbect[key].state)console.log(myMainOjbect[key].state)不一样

I don't understand why I can't just simply access an object within an object我不明白为什么我不能简单地访问 object 中的 object

You can.你可以。 See the snippet below for an example:有关示例,请参见下面的代码段:

 const myMainOjbect = { attachment: { handle: 'attachment', slug: 'Images', state: { success: true, data: { activated: false, info: { demo: 'demo-1', component: 'attachment', }, success: true, } } } }; const objectData = {}; for (const key in myMainOjbect) { objectData[key] = myMainOjbect[key]; } console.log(objectData); console.log(objectData.attachment.state); const myState = myMainOjbect.attachment.state; console.log(myState);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM