未定义的属性嵌套的 JavaScript 对象

Undefined Property Nested JavaScript Object

本文关键字:对象 JavaScript 属性 未定义 嵌套      更新时间:2023-09-26

我正在尝试从这个嵌套对象中获取值。似乎语法对于对象来说很好。我错过了什么?

console.log(thePosts['THE PARENT ONE']['sections']['ONE SECTION']['subs']['img'][3]);
window.thePosts = {}
window.thePosts = {
    'THE PARENT ONE' : {
        'url':'the-url',
        'sections': [{
            'ONE SECTION' : {
                'url':'making-money',
                'subs': [
                    {'_id:':'1','title': 'title 1','img': '<img src="/wp-content/assets/images/pop1.jpg">'},
                    {'_id:':'1','title': 'title 2','img': '<img src="/wp-content/assets/images/pop1.jpg">'},
                    {'_id:':'1','title': 'title 3','img': '<img src="/wp-content/assets/images/pop1.jpg">'},
                    {'_id:':'1','title': 'title 4','img': '<img src="/wp-content/assets/images/pop1.jpg">'}
                ]
            },
            'TWO SECTION' : {
                'url':'marketing',
                'subs': [
                    {'_id:':'1','title': 'title 1','img': '<img src="/wp-content/assets/images/pop1.jpg">'},
                    {'_id:':'1','title': 'title 2','img': '<img src="/wp-content/assets/images/pop1.jpg">'},
                    {'_id:':'1','title': 'title 3','img': '<img src="/wp-content/assets/images/pop1.jpg">'},
                    {'_id:':'1','title': 'title 4','img': '<img src="/wp-content/assets/images/pop1.jpg">'}
                ]
            }
        }]
    }
};

试试这个:

thePosts['THE PARENT ONE']['sections'][0]['ONE SECTION']['subs'][3]['img']

sectionssubs 都是数组。

sections是一个数组:

thePosts['THE PARENT ONE']['sections'][0]['ONE SECTION']['subs'][3]['img']
                                       ^                         ^----^