2014-05-02 166 views
0

任何人都可以,請給我一個替代語法以下訪問基於結構嵌套屬性

var id = '-JLxSeCPUCVN13FxifTY'; 
    var ResultsContainer = results[id]; 
    var i=0; 
    for(var k in ResultsContainer) 
    { 
    var TheArrayOfObjectsThatIneed = ResultsContainer[Object.keys(ResultsContainer)[i]]; 
    console.log(TheArrayOfObjectsThatIneed); 
    //loop the TheArrayOfObjectsThatIneed do the processing 
    i++; 
    } 

,你在圖片中看到我有一個對象中的對象內的數組,我不知道是什麼屬性名稱是結構總是相同的{results:{id:{idthatidontknow:[{}]}}}我需要的是訪問數組enter image description here

上面的代碼很好,但我是新的以javescript和我想知道是否有一個更好的語法,如果我這樣做是正確的方式

回答

1

也許這樣?

var id = '-JLxSeCPUCVN13FxifTY'; 
var ResultsContainer = results[id]; 
for(var k in ResultsContainer) { 
    if (ResultsContainer.hasOwnProperty(k)) { 
     var TheArrayOfObjectsThatIneed = ResultsContainer[k]; 
     console.log(TheArrayOfObjectsThatIneed); 
     //loop the TheArrayOfObjectsThatIneed do the processing 
    } 
} 
+0

沒有任何性能上的差異,我避免使用有自己的屬性,因爲有一個屬性只 – user1492051

+1

好一件事,你是不是取了ResultsContainer的每一個循環的關鍵。 –