如何从对象javascript中读取数据

时间:2016-05-23 05:37:19

标签: javascript

我有一个包含对象的对象evt.participators。登录时,我会显示以下结果: enter image description here

我想读取evt.participators对象的所有id。我试试这段代码:

 var t=0 ; 
 angular.forEach(evt.participators, function(value, key){
 console.log(evt.participators.$id  ); 
 t++; 
 });

但我有三次undefined。 我该怎么办呢?

2 个答案:

答案 0 :(得分:0)

您可以使用for...in循环

轻松检索对象中的所有键
for (var key in evt.participators) {
    if (evt.participators.hasOwnProperty(key)) {
        console.log(key);
    }
}

JsFiddle:https://jsfiddle.net/er647anm/1/

答案 1 :(得分:0)

使用for..in循环访问对象

for (var prop in evt.participators) {
  console.log(evt.participators[prop]);
}

有关详情,请点击For..In Documentation

相关问题