2 回答

TA貢獻2011條經驗 獲得超2個贊
只需遍歷對象數組即可。
var arr = [
{type: "????? ?????", num: 14},
{type: "??? ??????", num: 8},
{type: "??? ????????", num: 2},
{type: "??? ???????", num: 1},
{type: "??? ?????? ??????", num: 1},
{type: "??? ?????? ????????", num: 1},
{type: "??? ?????? ??????", num: 1}
];
var labels = [];
var datas = [];
for(const obj of arr){
labels.push(obj.type);
datas.push(obj.num);
}
console.log("Labels", labels);
console.log("Datas", datas);
對于您的特定情況,您需要在 AJAX 調用的回調中循環遍歷數組,因為 AJAX 是異步的。
$.get('../Functions/Ajax/GetSortingData.php?id='+schoolId, function(data){
jsonData = JSON.parse(data);
console.log(jsonData);
var labels = [];
var datas = [];
for(const obj of jsonData){
labels.push(obj.type);
datas.push(obj.num);
}
console.log(datas);
});
添加回答
舉報