Reputation: 9399
I have the following object:
{
"EekvB3cnwEzE":{
"name":"hi",
},
"Brv1R4C6bZnD":{
"name":"yup",
},
"kRwRXju6ALJZ":{
"name":"okay",
}
}
I am trying to add each of these items into an array. I do the following code but for some reason, I get back []
in my console.log
Could anyone help me figure out what's wrong?
$scope.items = [];
$http.get("/url").success(function(data) {
$.each(data, function(key, value) { $scope.items[key] = value; });
console.log($scope.items);
});
Upvotes: 0
Views: 54
Reputation: 944529
Named keys will be ignored when you log an array.
If you want them to show up there, you'll need to replace key
(in this bit of code $scope.items[key]
) with a number or use a .push(value)
instead of an assignment.
Upvotes: 4
Reputation: 49949
There must be something wrong with your ajax call. Because the following works:
var x = [];
$.each({
"EekvB3cnwEzE":{
"name":"hi",
},
"Brv1R4C6bZnD":{
"name":"yup",
},
"kRwRXju6ALJZ":{
"name":"okay",
}
}, function(key, value){
x[key] = value;
});
console.log(x);
It will log:
[EekvB3cnwEzE: Object, Brv1R4C6bZnD: Object, kRwRXju6ALJZ: Object]
Upvotes: 0