简体   繁体   English

将json的一部分提取到另一个对象中,并保持两者之间的链接

[英]extract part of json into another object and keep link between both

{
   "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"
      }
   ] 
}

what i want is to create another array out of this including vt_name,vt_color and index of data so i use jquery 我想要的是从中创建另一个数组,包括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] is not defined.... 失败:未定义单词[this.do_id]。

how can i do this ? 我怎样才能做到这一点 ? i want outcome to be 我希望结果是

sorry i'm a newbie with json/js 抱歉,我是json / js的新手

i want outcome in php would be 我希望在php中的结果是

['00002'=>[
            ['name'=>'follow up','color'=>'#548dd4',index=>[0,3,4]],
            ['name'=>'ultrasound','color'=>'#769dd4',index=>[1,5,8]]
          ]
 ]
  1. Change the type of words to object (not array), since you want a map with a string (eg '00002') as key: 将单词的类型更改为对象(而不是数组),因为您希望使用带有字符串(例如“ 00002”)的映射作为键:

     var words = {}; 
  2. You want do add to an array which does not exist. 您要添加到不存在的数组中。 You need to create it first: 您需要先创建它:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM