2013-07-30 77 views
0

我試圖從這個嵌套對象中獲取一個值。似乎該對象的語法很好。我錯過了什麼?未定義的屬性嵌套的JavaScript對象

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">'} 
       ] 
      } 
     }] 
    } 
}; 
+1

我猜你的意思是'[ '潛艇'] [3] [ 'IMG']'? – Danny

+1

'thePosts ['THE PARENT ONE'] ['sections']'是一組對象! – Bergi

回答

2

試試這個:

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

sectionssubs都陣列。

2

sections是一個數組:

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