Javascript对象键/值返回undefined

时间:2016-11-10 20:29:08

标签: javascript json object

我使用$.each语句循环遍历 JSON ,创建对象后,它在console.log上正确显示,但是说出来了没有钥匙。以下是正在使用的方法和console.log输出。

JS :(这是$.extend(Account.prototype)创建方法

getDuration: function(type){
    var data = {},
        type = type;

    $.get('/service/account/ajaxdata?method=duration&type='+type, function(response){
        console.log(response);
        $.each(JSON.parse(response), function(k,v){
            data[v.type] = v.duration;
        });
    });
    return data;
},

getUserRates: function(){
    var duration = this.getDuration('user');

    console.log(duration);       
    console.log(duration['ADDUSER']); 
}

以下是console.log函数的输出

response:
[{"name":"SUBSCRIPTION","duration":"30","id":"2","type":"BASICSUB","lightning_radius":null,"description":"Basic Subscription"},{"name":"ADDUSER","duration":"30","id":"3","type":"ADDUSER","lightning_radius":null,"description":"Additional User(s)"}]

duration:
Object
     ADDUSER : "30"
     BASICSUB : "30"

duration['ADDUSER']:
undefined

只是为了它,Object.keys(duration)返回{},一个空白对象。我在哪里搞砸了ADDUSER键的值?

0 个答案:

没有答案