简体   繁体   中英

How can I remove object from multidimensional array javascript?

My case is bit different from other cases

I have an object that contains an array like this :

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",
    }
  ]
};

I want to add the isExistScedule key to the array. isExistSchedule key is obtained from vuex store. In this case, I will show you the ExistScedule which is hardcoded so that you can understand more easily

My code like this :

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

I want when isExistScedule = false, it will delete the elements that are in the group

So I want the final result to be like this :

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",
    }
  ]
};

How do I make the code so the result look like that?

I have tried it, but it hasn't worked. this case is really complicated

you can use Array.prototype.filter to filter out the elements

 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)

Using simple for-loop

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

console.log(updatedDocs)

Using the code you provided, it can be tweaked a little bit to be sure only the doctors with existing schedules will be returned.

Setting the isExistSchedule in a static way simplifies the problem a bit. To handle the data as is, we can run a single loop (technically a second inner loop with 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;
  }
}

Runnable example:

 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>


Refactoring

As you noted, the data is complicated but that part can be remedied by changing some redundancies.

The first change which could help is to change the doctors object so that the doctor_id is only listed once (the subsequent array of objects contains the doctor_id in each)

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

And with that, we should probably also change it so that the array of objects are changed to an object with a simplified array:

'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"
    }
  ],
},

And, if you wanted to separate out the data such that there's a separate key of Hospital IDs and Specialization IDs, you could just include the IDs of each in the doctors object:

'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"
  ]
},

Using the optimizations above makes it easier to read and handle in your program. See the below modified solution for returning only doctors with 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>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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