I'm using a $.each
statement to loop through JSON and after creating the object, it's being displayed correctly on the console.log, but says it has no keys. Below are the methods being used and the console.log output.
JS: (This is in a $.extend(Account.prototype)
to create the methods
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']);
}
Below is the output of the console.log function
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
And just for the sake of it, Object.keys(duration) returns {}, a blank object. Where am I messing up to grab the value of ADDUSER key?