Alko Alko - 7 days ago 6
jQuery Question

jQuery, JSON array push

Im having a little problem making this work:

Here is my json array pulled via ajax:

{
"message": [{
"title": "Account",
"id": 1
}, {
"title": "Content",
"id": 2
}, {
"title": "Other",
"id": 3
}]
}


here is javascript:

var items = [];
$.get("settings.php", {
getlink: 1,
}, function(json) {
$.each(json.message, function() {
items.push(this);
});

},"json");

console.log(items)


But for some reason items array is always empty []
I can see in firebug, json returned array, but I'm not able to push it.

Answer

Use the index, value that $.each return :

$.each(json.message, function(index, value) {
    items.push(value);
});

NOTE : $.each() is different than .each().

Hope this helps.