2015-12-13 171 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数据中包含(订购的)作者列表。