0
{
   "data": [
      {
         "date": "2013-03-07",
         "id": "2",
         "vt_color": "#548dd4",
         "vt_name": "follow up",
         "duration": "20",
         "time_booked": "12:00:00",
         "stats": "booked",
         "doctor_id": "00002",
         "patient_id": "00003"
      },
      {
         "date": "2013-03-08",
         "id": "3",
         "vt_color": "#76923c",
         "vt_name": "ultrasound",
         "duration": "30",
         "time_booked": "08:00:00",
         "stats": "booked",
         "pt_name": "demo patien",
         "dr_name": "Momen Alzalabany",
         "doctor_id": "00002",
         "patient_id": "00009"
      }
   ] 
}

我想要的是从中创建另一个数组,包括 vt_name、vt_color 和数据索引,所以我使用 jquery

var words = [];

$.each(arr['data'],function(ref){
    words[this.doctor_id].push({name: this.vt_color,color: this.vt_name,index:ref});
});
console.log(koko);

FAIL : words[this.do_id] 未定义....

我怎样才能做到这一点 ?我希望结果是

抱歉,我是 json/js 的新手

我希望 php 的结果是

['00002'=>[
            ['name'=>'follow up','color'=>'#548dd4',index=>[0,3,4]],
            ['name'=>'ultrasound','color'=>'#769dd4',index=>[1,5,8]]
          ]
 ]
4

1 回答 1

0
  1. 将单词的类型更改为对象(不是数组),因为您想要一个以字符串(例如 '00002')为键的映射:

    var words = {};
    
  2. 您想要添加到不存在的数组中。您需要先创建它:

    words[this.doctor_id] = words[this.doctor_id] || []; // creates an array if not already existing
    words[this.doctor_id].push({name: this.vt_color,color: this.vt_name,index:ref});
    
于 2013-03-06T14:40:56.893 回答