2014-03-24 26 views
1

我正在迭代通过json数据...但我无法获取console.log中的值 你能告诉我如何解决它.. 我写的是正确的语法我的控制台.. 提供下面我的代码...console.log不显示js值

http://jsfiddle.net/7Bn63/4/

function allProfile() { 
    for (var i = 0; i < allProfile.length; i++) { 
     console.log("i am here"); 
     console.log(allProfile[i].Class of Service 1); 
    } 
} 

var allProfile = [{ 
    Profile: 101, 
     'Class of Service 1': '90%' 

}]; 
+6

'allProfile [1]。[ “服务等级1”]​​'? – DontVoteMeDown

回答

7

你必须使用括号记号来访问属性

allProfile[i]['Class of Service 1'] 

和你的函数具有相同的名称为对象,所以它的覆盖

function iterator() { 
    for (var i = 0; i < allProfile.length; i++) { 
     console.log(allProfile[i]['Class of Service 1']); 
    } 
} 

var allProfile = [{ 
    Profile: 101, 
    'Class of Service 1': '90%' 
}]; 

iterator(); 

FIDDLE