2017-05-11 49 views
0

我正在選擇一個醫生列表,我想在農村地區的一個部門進行分組。然後將選定的醫生推入陣列並保存到數據庫中。我可以選擇併發布以保存在數據庫中,但是我發現有一些缺陷難以解決。 我的問題是,當我選擇醫生1和醫生2併發布到我的服務,發送到我的服務的參數被計爲3.這是醫生1,醫生2,醫生2.爲什麼醫生2被複制?Http Post Json - 陣列

選擇醫生

selectDoctors(Doctors){ 
      Doctors.listed = (Doctors.listed) ? false : true; 
      this.deptList = Doctors 

     } 

提交選定醫生的組中的陣列

 Doctors: Doctors[] = []; 

     hospital = { 
       hospital_name : "", 

       details : [{ 
      id: "", 
      ward: "", 
     }] 
    } 


    //submit data into array 

    Object.keys(this.Doctors).filter(key => this.Doctors[key].selected) 
       .forEach(key => { 



        this.hospital.details['0'].id = this.Doctors[key].id 
        this.hospital.details['0'].ward =this.Doctors[key].ward 

        this.groups.members.push(this.Doctors.['0']); 

} 
     this.http.categorizeDepartment(this.groups) 
     .subscribe(data => { 

}); 

} 
+0

你可以plase console.log()內的forEach部分,看看它得到迭代3次嗎? – zenwraight

+0

你也試圖用數組中的最後一名醫生覆蓋hospital.details嗎?或者,您是否打算將每個醫生的ID和病房添加到陣列中的單獨對象中? – Eeks33

回答

0

看起來像有一些奇怪的突變和在這裏所使用的不必要的方法,結果是不可預知的。如果你想要做的就是把選擇的醫生爲一組,你可以這樣做:

this.groups.members = [ 
    ...this.groups.members, 
    this.Doctors.filter(doctor => doctor.selected) 
]; 

如果你也想加入到hospital.details陣列中的第一項醫生的信息,您可以添加那也是:

let selectedDoctors = this.Doctors.filter(doctor => doctor.selected); 

this.groups.members = [...this.groups.members, selectedDoctors]; 

for (let doctor of selectedDoctors) { 
    this.hospital.details['0'].id = doctor.id 
    this.hospital.details['0'].ward = doctor.ward 
}