簡體   English   中英

如何從多維數組javascript中刪除對象?

[英]How can I remove object from multidimensional array javascript?

我的情況與其他情況有點不同

我有一個包含這樣的數組的對象:

var doctors = {
  '2bf169c6-bc51-4dc6-1234-cf81e611b6fd': [
    {
      doctor_id: "2bf169c6-bc51-4dc6-1234-cf81e611b6fd",
      name: "dr. Benjamin",
      specialization_id: "5f8e2525-65fa-4a92-2312-fcd0323ad439",
      specialization_name: "General practitioners",
      hospital_id: "63c6af56-bb9a-4962-6677-454d3345630d",
      hospital_name: "Hospital A"
    }
  ],
  'd8e31868-ebec-4af0-4321-767aa696f91a': [
    {
      doctor_id: "d8e31868-ebec-4af0-4321-767aa696f91a",
      name: "dr. Theo",
      specialization_id: "329de195-1ab6-4a43-3322-acfde447fec3",
      specialization_name: "Internal Medicine",
      hospital_id: "39764039-37b9-4176-4455-ef7b2e124ba4",
      hospital_name: "Hospital B"
    },
    {
      doctor_id: "d8e31868-ebec-4af0-4321-767aa696f91a",
      name: "dr. Theo",
      specialization_id: "329de195-1ab6-4a43-3322-acfde447fec3",
      specialization_name: "Internal Medicine",
      hospital_id: "65a60870-beab-4925-3322-4a5246e26d6a",
      hospital_name: "Hospital C"
    }
  ], 
  '0a24e765-3e3c-45e6-1122-8671eb3c0439': [
    {
      doctor_id: "0a24e765-3e3c-45e6-1122-8671eb3c0439",
      name: "dr. John",
      specialization_id: "5f8e2525-65fa-4a92-2312-fcd0323ad439",
      specialization_name: "General practitioners",
      hospital_id: "153b75ee-dc07-4290-2121-d4d28457780f",
      hospital_name: "Hospital D",
    },
    {
      doctor_id: "0a24e765-3e3c-45e6-1122-8671eb3c0439",
      name: "dr. John",
      specialization_id: "5f8e2525-65fa-4a92-2312-fcd0323ad439",
      specialization_name: "General practitioners",
      hospital_id: "153b75ee-dc07-4290-2121-d4d28457780f",
      hospital_name: "Hospital E",
    }
  ],
  '4990a698-afba-483b-5544-ecc5201e45c3': [
    {
      doctor_id: "4990a698-afba-483b-5544-ecc5201e45c3",
      name: "dr. Frank",
      specialization_id: "97ce4804-6508-4d65-dd33-70a3d14604ae",
      specialization_name: "Neurologist",
      hospital_id: "c279e92e-57c3-47ad-5656-397b4fe8b6f7",
      hospital_name: "Hospital F",
    }
  ]
};

我想將 isExistScedule 鍵添加到數組中。 isExistSchedule 鍵是從 vuex 商店獲得的。 在這種情況下,我將向您展示硬編碼的 ExistScedule,以便您更容易理解

我的代碼是這樣的:

const newDoctors = {}
for (let item in doctors) {
  for (let i = 0; i < doctors[item].length; i++) {
    // const paramsSchedule = {
    //   hospitalId: doctors[item][i].hospital_id,
    //   doctorId: doctors[item][i].doctor_id
    // }
    // const promiseSchedule = this.getDataSchedule(paramsSchedule) // call vuex store and api
    // await promiseSchedule
    // const dataSchedule = this.dataSchedule.items
    // doctors[item][i].isExistSchedule = dataSchedule.isExistSchedule
    /* below I display the data statically so that you more easily understand */
    doctors['2bf169c6-bc51-4dc6-1234-cf81e611b6fd'][0].isExistSchedule = false
    doctors['d8e31868-ebec-4af0-4321-767aa696f91a'][0].isExistSchedule = true
    doctors['d8e31868-ebec-4af0-4321-767aa696f91a'][1].isExistSchedule = false
    doctors['0a24e765-3e3c-45e6-1122-8671eb3c0439'][0].isExistSchedule = true
    doctors['0a24e765-3e3c-45e6-1122-8671eb3c0439'][1].isExistSchedule = true
    doctors['4990a698-afba-483b-5544-ecc5201e45c3'][0].isExistSchedule = true

  }
  newDoctors[item] = doctors[item]
}

我想要當 isExistScedule = false 時,它​​會刪除組中的元素

所以我希望最終的結果是這樣的:

var doctors = {
  'd8e31868-ebec-4af0-4321-767aa696f91a': [
    {
      doctor_id: "d8e31868-ebec-4af0-4321-767aa696f91a",
      name: "dr. Theo",
      specialization_id: "329de195-1ab6-4a43-3322-acfde447fec3",
      specialization_name: "Internal Medicine",
      hospital_id: "39764039-37b9-4176-4455-ef7b2e124ba4",
      hospital_name: "Hospital B"
    },
  ], 
  '0a24e765-3e3c-45e6-1122-8671eb3c0439': [
    {
      doctor_id: "0a24e765-3e3c-45e6-1122-8671eb3c0439",
      name: "dr. John",
      specialization_id: "5f8e2525-65fa-4a92-2312-fcd0323ad439",
      specialization_name: "General practitioners",
      hospital_id: "153b75ee-dc07-4290-2121-d4d28457780f",
      hospital_name: "Hospital D",
    },
    {
      doctor_id: "0a24e765-3e3c-45e6-1122-8671eb3c0439",
      name: "dr. John",
      specialization_id: "5f8e2525-65fa-4a92-2312-fcd0323ad439",
      specialization_name: "General practitioners",
      hospital_id: "153b75ee-dc07-4290-2121-d4d28457780f",
      hospital_name: "Hospital E",
    }
  ],
  '4990a698-afba-483b-5544-ecc5201e45c3': [
    {
      doctor_id: "4990a698-afba-483b-5544-ecc5201e45c3",
      name: "dr. Frank",
      specialization_id: "97ce4804-6508-4d65-dd33-70a3d14604ae",
      specialization_name: "Neurologist",
      hospital_id: "c279e92e-57c3-47ad-5656-397b4fe8b6f7",
      hospital_name: "Hospital F",
    }
  ]
};

如何使代碼看起來像這樣?

我試過了,但沒有奏效。 這個案子真的很復雜

您可以使用Array.prototype.filter過濾掉元素

 let doctors = { '2bf169c6-bc51-4dc6-1234-cf81e611b6fd': [ { doctor_id: '2bf169c6-bc51-4dc6-1234-cf81e611b6fd', name: 'dr. Benjamin', specialization_id: '5f8e2525-65fa-4a92-2312-fcd0323ad439', specialization_name: 'General practitioners', hospital_id: '63c6af56-bb9a-4962-6677-454d3345630d', hospital_name: 'Hospital A', isExistSchedule: false } ], 'd8e31868-ebec-4af0-4321-767aa696f91a': [ { doctor_id: 'd8e31868-ebec-4af0-4321-767aa696f91a', name: 'dr. Theo', specialization_id: '329de195-1ab6-4a43-3322-acfde447fec3', specialization_name: 'Internal Medicine', hospital_id: '39764039-37b9-4176-4455-ef7b2e124ba4', hospital_name: 'Hospital B', isExistSchedule: true }, { doctor_id: 'd8e31868-ebec-4af0-4321-767aa696f91a', name: 'dr. Theo', specialization_id: '329de195-1ab6-4a43-3322-acfde447fec3', specialization_name: 'Internal Medicine', hospital_id: '65a60870-beab-4925-3322-4a5246e26d6a', hospital_name: 'Hospital C', isExistSchedule: false } ], '0a24e765-3e3c-45e6-1122-8671eb3c0439': [ { doctor_id: '0a24e765-3e3c-45e6-1122-8671eb3c0439', name: 'dr. John', specialization_id: '5f8e2525-65fa-4a92-2312-fcd0323ad439', specialization_name: 'General practitioners', hospital_id: '153b75ee-dc07-4290-2121-d4d28457780f', hospital_name: 'Hospital D', isExistSchedule: true }, { doctor_id: '0a24e765-3e3c-45e6-1122-8671eb3c0439', name: 'dr. John', specialization_id: '5f8e2525-65fa-4a92-2312-fcd0323ad439', specialization_name: 'General practitioners', hospital_id: '153b75ee-dc07-4290-2121-d4d28457780f', hospital_name: 'Hospital E', isExistSchedule: true } ], '4990a698-afba-483b-5544-ecc5201e45c3': [ { doctor_id: '4990a698-afba-483b-5544-ecc5201e45c3', name: 'dr. Frank', specialization_id: '97ce4804-6508-4d65-dd33-70a3d14604ae', specialization_name: 'Neurologist', hospital_id: 'c279e92e-57c3-47ad-5656-397b4fe8b6f7', hospital_name: 'Hospital F', isExistSchedule: true } ] } let updatedDocs= Object.fromEntries(Object.entries({ ...doctors }).map(([key, value]) => ([key, [...value].filter(e => e.isExistSchedule)])).filter(e => e[1].length)) console.log(updatedDocs)

使用簡單的for-loop

for (let doc in doctors) {
  let arr = [...doctors[doc].filter(item => item.isExistSchedule)];
  if(arr.length)
    UpdatedDocs[doc] = arr;
}

console.log(updatedDocs)

使用您提供的代碼,可以稍微調整一下,以確保只返回具有現有時間表的醫生。

以靜態方式設置isExistSchedule簡化問題。 為了按原樣處理數據,我們可以運行一個循環(技術上是第二個帶filter內部循環):

const newDoctors = {};
for (let doctor_id in doctors) {
  /* Create an array to hold values where `isExistSchedule` is true */
  let newDoctor = [];
  /* If the current item's `isExistSchedule` is true, it will be included in our array */
  let items = doctors[doctor_id].filter((item) => item.isExistSchedule);
  if (items.length > 0) {
    /* If items is not empty, add it to our `newDoctors` object */
    newDoctors[doctor_id] = items;
  }
}

可運行示例:

 const output = document.querySelector("output"); const doctors = { '2bf169c6-bc51-4dc6-1234-cf81e611b6fd': [{ doctor_id: "2bf169c6-bc51-4dc6-1234-cf81e611b6fd", name: "dr. Benjamin", specialization_id: "5f8e2525-65fa-4a92-2312-fcd0323ad439", specialization_name: "General practitioners", hospital_id: "63c6af56-bb9a-4962-6677-454d3345630d", hospital_name: "Hospital A" }], 'd8e31868-ebec-4af0-4321-767aa696f91a': [{ doctor_id: "d8e31868-ebec-4af0-4321-767aa696f91a", name: "dr. Theo", specialization_id: "329de195-1ab6-4a43-3322-acfde447fec3", specialization_name: "Internal Medicine", hospital_id: "39764039-37b9-4176-4455-ef7b2e124ba4", hospital_name: "Hospital B" }, { doctor_id: "d8e31868-ebec-4af0-4321-767aa696f91a", name: "dr. Theo", specialization_id: "329de195-1ab6-4a43-3322-acfde447fec3", specialization_name: "Internal Medicine", hospital_id: "65a60870-beab-4925-3322-4a5246e26d6a", hospital_name: "Hospital C" } ], '0a24e765-3e3c-45e6-1122-8671eb3c0439': [{ doctor_id: "0a24e765-3e3c-45e6-1122-8671eb3c0439", name: "dr. John", specialization_id: "5f8e2525-65fa-4a92-2312-fcd0323ad439", specialization_name: "General practitioners", hospital_id: "153b75ee-dc07-4290-2121-d4d28457780f", hospital_name: "Hospital D", }, { doctor_id: "0a24e765-3e3c-45e6-1122-8671eb3c0439", name: "dr. John", specialization_id: "5f8e2525-65fa-4a92-2312-fcd0323ad439", specialization_name: "General practitioners", hospital_id: "153b75ee-dc07-4290-2121-d4d28457780f", hospital_name: "Hospital E", } ], '4990a698-afba-483b-5544-ecc5201e45c3': [{ doctor_id: "4990a698-afba-483b-5544-ecc5201e45c3", name: "dr. Frank", specialization_id: "97ce4804-6508-4d65-dd33-70a3d14604ae", specialization_name: "Neurologist", hospital_id: "c279e92e-57c3-47ad-5656-397b4fe8b6f7", hospital_name: "Hospital F", }] }; doctors['2bf169c6-bc51-4dc6-1234-cf81e611b6fd'][0].isExistSchedule = false doctors['d8e31868-ebec-4af0-4321-767aa696f91a'][0].isExistSchedule = true doctors['d8e31868-ebec-4af0-4321-767aa696f91a'][1].isExistSchedule = false doctors['0a24e765-3e3c-45e6-1122-8671eb3c0439'][0].isExistSchedule = true doctors['0a24e765-3e3c-45e6-1122-8671eb3c0439'][1].isExistSchedule = true doctors['4990a698-afba-483b-5544-ecc5201e45c3'][0].isExistSchedule = true const newDoctors = {}; for (let doctor_id in doctors) { let newDoctor = []; let items = doctors[doctor_id].filter((item) => item.isExistSchedule); if (items.length > 0) { newDoctors[doctor_id] = items; } // else { // don't include the doctor in `newDoctors` //} } output.innerHTML += `<pre>${JSON.stringify(newDoctors, null, 2)}</pre>`;
 <output></output>


重構

正如您所指出的,數據很復雜,但可以通過更改一些冗余來彌補這一部分。

第一個可能有幫助的更改是更改醫生對象,以便僅列出一次doctor_id (隨后的對象數組在每個對象中都包含doctor_id

var doctors = {
  '2bf169c6-bc51-4dc6-1234-cf81e611b6fd': [
    {
      name: "dr. Benjamin",
// ...

有了這個,我們可能還應該更改它,以便將對象數組更改為具有簡化數組的對象:

'd8e31868-ebec-4af0-4321-767aa696f91a': {
  name: "dr. Theo",
  specialization_id: "329de195-1ab6-4a43-3322-acfde447fec3",
  specialization_name: "Internal Medicine",
  hospitals: [
    {
      hospital_id: "39764039-37b9-4176-4455-ef7b2e124ba4",
      hospital_name: "Hospital B"
    },
    {
      hospital_id: "65a60870-beab-4925-3322-4a5246e26d6a",
      hospital_name: "Hospital C"
    }
  ],
},

而且,如果您想分離數據,以便有一個單獨的醫院 ID 和專業 ID 鍵,您可以只在doctors對象中包含每個的 ID:

'd8e31868-ebec-4af0-4321-767aa696f91a': {
  name: "dr. Theo",
  specialization_id: "329de195-1ab6-4a43-3322-acfde447fec3",
  hospitals: [
    "39764039-37b9-4176-4455-ef7b2e124ba4",
    "65a60870-beab-4925-3322-4a5246e26d6a"
  ]
},

使用上述優化可以更容易地閱讀和處理您的程序。 請參閱以下修改后的解決方案,以僅返回帶有isExistSchedule醫生:

 const output = document.querySelector("output"); var doctors = { '2bf169c6-bc51-4dc6-1234-cf81e611b6fd': { name: 'dr. Benjamin', specialization_id: '5f8e2525-65fa-4a92-2312-fcd0323ad439', hospitals: [{ id: '63c6af56-bb9a-4962-6677-454d3345630d' }], }, 'd8e31868-ebec-4af0-4321-767aa696f91a': { name: 'dr. Theo', specialization_id: '329de195-1ab6-4a43-3322-acfde447fec3', hospitals: [{ id: '39764039-37b9-4176-4455-ef7b2e124ba4' }, { id: '65a60870-beab-4925-3322-4a5246e26d6a' }, ], }, '0a24e765-3e3c-45e6-1122-8671eb3c0439': { name: 'dr. John', specialization_id: '5f8e2525-65fa-4a92-2312-fcd0323ad439', hospitals: [{ id: '153b75ee-dc07-4290-2121-d4d28457780f' }, { id: '153b75ee-dc07-4290-2121-d4d28457780f', } /* Hospital E (duplicate id? or same hospital?) */ , ], }, '4990a698-afba-483b-5544-ecc5201e45c3': { name: 'dr. Frank', specialization_id: '97ce4804-6508-4d65-dd33-70a3d14604ae', hospitals: [{ id: 'c279e92e-57c3-47ad-5656-397b4fe8b6f7' }], }, }; var hospitals = { '63c6af56-bb9a-4962-6677-454d3345630d': 'Hospital A', '39764039-37b9-4176-4455-ef7b2e124ba4': 'Hospital B', '153b75ee-dc07-4290-2121-d4d28457780f': 'Hospital D', /* '153b75ee-dc07-4290-2121-d4d28457780f': 'Hospital E', // Duplicate hospital_id, different hospital name? */ 'c279e92e-57c3-47ad-5656-397b4fe8b6f7': 'Hospital F', }; var specializations = { '5f8e2525-65fa-4a92-2312-fcd0323ad439': 'General practitioners', '329de195-1ab6-4a43-3322-acfde447fec3': 'Internal Medicine', '97ce4804-6508-4d65-dd33-70a3d14604ae': 'Neurologist', }; doctors['2bf169c6-bc51-4dc6-1234-cf81e611b6fd'].hospitals[0].isExistSchedule = false; doctors['d8e31868-ebec-4af0-4321-767aa696f91a'].hospitals[0].isExistSchedule = true; doctors['d8e31868-ebec-4af0-4321-767aa696f91a'].hospitals[1].isExistSchedule = false; doctors['0a24e765-3e3c-45e6-1122-8671eb3c0439'].hospitals[0].isExistSchedule = true; doctors['0a24e765-3e3c-45e6-1122-8671eb3c0439'].hospitals[1].isExistSchedule = true; doctors['4990a698-afba-483b-5544-ecc5201e45c3'].hospitals[0].isExistSchedule = true; const newDoctors = {}; for (let doctor_id in doctors) { let { name, specialization_id, hospitals } = doctors[doctor_id]; hospitals = hospitals.filter((hospital) => { return hospital.isExistSchedule; }); if (hospitals.length > 0) { // if you want to include `isExistSchedule`, comment out the following line hospitals = hospitals.map((hospital) => hospital.id); let newDoctor = { name, specialization_id, hospitals }; newDoctors[doctor_id] = newDoctor; } } output.innerHTML += `<div>Note: there are only 3 entries, but the second entry has two 'hospital' array entries.</div><pre>${JSON.stringify(newDoctors, null, 2)}</pre>`;
 <output></output>

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM