2015-12-13 173 views
2

我有一個這樣的對象:獲取從對象的值

{ 
    " Fairfield, D. H." : [0, 0, 3, 3, 2, 2], 
    " Mish, W. H."  : [3, 0, 3, 3, 2, 2], 
    " Baker, D. N."  : [0, 0, 0, 0, 2, 2], 
    " Curtis, S. A." : [0, 0, 3, 0, 2, 2], 
    "Ocuna, M. H."  : [0, 0, 0, 0, 0, 0], 
    " Ogilvie, K. W." : [0, 0, 0, 0, 2, 0] 
} 

所以在我的對象(例如第一線)key" Fairfield, D. H."value[0, 0, 3, 3, 2, 2]

調用我的代碼第三行我必須使用其密鑰作爲以下內容:

console.log(myObj[" Baker, D. N."]); 

但我不想調用該行b Ÿ關鍵,我想它的指數例如叫它:

console.log(myObj[2]); 

,然後獲得在我的例子中該行的關鍵是" Baker, D. N."

我該怎麼做?

編輯1:

,因爲我收到的對象由json電話,所以我必須在它的工作,因爲它是我不能跟一個數組,而不是工作。

該對象的含義是,這些鍵是作者的名字,值是其他作者與其他作者有多少關係。

例如筆者" Fairfield, D. H."有自己0關係,以「Mish, W. H."3關係基本與" Baker, D. N."0關係.....

我想要做的是創造與作者的名字的數組和這些作者之間的關係,另一個數組所以最後它必須是這個樣子:

nodes = [ 
    " Fairfield, D. H.", 
     " Mish, W. H.", 
     " Baker, D. N.", 
     " Curtis, S. A.", 
     "Ocuna, M. H.", 
     " Ogilvie, K. W." 
    ] 

edges = [ 
    ["Fairfield, D. H.", " Baker, D. N.", 3], 
    ["Fairfield, D. H.", " Curtis, S. A.", 3], 
    ["Fairfield, D. H.", "Ocuna, M. H.", 2], 
    ["Fairfield, D. H.", " Ogilvie, K. W.", 2], 
    [" Mish, W. H.", "Fairfield, D. H.", 3], 
    [" Mish, W. H.", " Baker, D. N.", 3], 
    [" Mish, W. H.", " Curtis, S. A.", 3], 
    [" Mish, W. H.", "Ocuna, M. H.", 2], 
    ......... 
    ] 

在我的代碼,我有這樣的事情:

$http.get('data/graphAuteur.JSON').then(function(response) { 

    var nodes = []; 
    var edges = []; 

    angular.forEach(response.data, function(authorRelations, authorName) { 
     nodes.push(authorName.trim()); 

      angular.forEach(authorRelations, function(relation, relationIndex) { 
       if (relation != 0) { 
        edges.push([authorName.trim(),relationIndex,relation]); 
       } 
      }); 


    }); 
    console.log(edges); 
} 

在控制檯這個樣子:

edges = [ 
     ["Fairfield, D. H.", 2, 3], 
     ["Fairfield, D. H.", 3, 3], 
     ["Fairfield, D. H.", 4, 2], 
     ["Fairfield, D. H.", 5, 2], 
     [" Mish, W. H.", 0, 3], 
     [" Mish, W. H.", 2, 3], 
     [" Mish, W. H.", 3, 3], 
     [" Mish, W. H.",4, 2], 
     ......... 
     ] 

所以我需要的是在該行edges.push([authorName.trim(),relationIndex,relation]);relationIndex改變這樣的事情response.data[relationIndex][0]因此,例如,如果relationIndex2無論如若response.data[relationIndex][0]或返回字符串" Baker, D. N."

+0

對象使用'鍵 - 值'對。 改用'array'(如果你想有索引)。 –

+0

這些鍵沒有排序,所以可能看起來像「第三個」鍵,可能不是你循環播放的第三個鍵。 –

+0

如果您只是遍歷列表,您可能需要查看for-in循環。 '(for key){/ * do stuff * /}' – TheCrzyMan

回答

0

您可以使用此代碼提取作者的名單:

var data = { 
    " Fairfield, D. H." : [0, 0, 3, 3, 2, 2], 
    " Mish, W. H."  : [3, 0, 3, 3, 2, 2], 
    " Baker, D. N."  : [0, 0, 0, 0, 2, 2], 
    " Curtis, S. A." : [0, 0, 3, 0, 2, 2], 
    "Ocuna, M. H."  : [0, 0, 0, 0, 0, 0], 
    " Ogilvie, K. W." : [0, 0, 0, 0, 2, 0] 
}; 

var authors = []; 
for (var name in o) authors.push(name); 

現在你有authors

[" Fairfield, D. H.", " Mish, W. H.", " Baker, D. N.", 
" Curtis, S. A.", "Ocuna, M. H.", " Ogilvie, K. W."] 

然而,隨着bhspencer wrote,也不能保證訂貨會與你的預期相同,所以你的數字可能不適合你期望的地方。

最好在json數據中包含(訂購的)作者列表。