亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

將 Json 數組鍵值轉換為 js 數組

將 Json 數組鍵值轉換為 js 數組

汪汪一只貓 2022-10-13 16:14:58
$AllSorts = array();$AllSorts[] = array('type'=>'????? ?????','num'=>$allSize);$AllSorts[] = array('type'=>'??? ??????','num'=>$studentSize);$AllSorts[] = array('type'=>'??? ????????','num'=>$tchSize);$AllSorts[] = array('type'=>'??? ???????','num'=>$managerSize);$AllSorts[] = array('type'=>'??? ?????? ??????','num'=>$atsSize);$AllSorts[] = array('type'=>'??? ?????? ????????','num'=>$bgrSize);$AllSorts[] = array('type'=>'??? ?????? ??????','num'=>$fgrSize);$JsonData = json_encode($AllSorts);echo $JsonData;這是獲取 thw json 日期的 php 代碼var jsonData = '';    $.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);這是javascript
查看完整描述

2 回答

?
嚕嚕噠

TA貢獻1784條經驗 獲得超7個贊

使用地圖

const array = [{

    type: "????? ?????",

    num: 14

  },

  //.snip

]


const type = array.map(i => i['type'])

console.log(type)


const num = array.map(i => i['num'])

console.log(num)



查看完整回答
反對 回復 2022-10-13
?
森林海

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);

       });


查看完整回答
反對 回復 2022-10-13
  • 2 回答
  • 0 關注
  • 157 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號