0
我在使用.push()時遇到了一些問題。我相信問題在於我如何定義變量。基本上,變量'cache'鍵不對應於來自JSON的data.system []。我試圖把一些使用這個(縮寫代碼版本)現有陣列:將值推送到子陣
var cache = ['cpu','mem'];
function doAjax() { /* toggled when ajax button pressed */
$.getJSON("ajax.php", function(data) {
$.each(data.system, function(i, key) {
console.log(cache.i); /* undefined logged */
cache.i.push(key.value); /*Uncaught TypeError: Cannot call method 'push' of undefined */
});
});
}
JSON:
{
system: {
cpu: {
value: 1
},
mem: {
value: 330
}
}
}
小提琴(我不認爲我設置的JSON數據正確雖然):http://jsfiddle.net/x9JaP/
我看到你的json結構中沒有數組。 –
我遺漏了外面的{}。 JSON完美適用於其他代碼領域。 – user3155229