1

I am trying to remove a number of properties from an object of objects I've found a lot of questions on stack overflow about doing this for array but I can't seem to figure out a clean way of going about doing this for my purposes.

My data looks like this:

{ 
emr: {ID: "user-1504966781-340782", languageDesc: "English", orgId: 1504966781,…}
pcc: {ID: "user-1504966781-340782", languageDesc: "English", orgId: 1504966781,…}
}

The goal here is to remove let's say languageDesc, and orgID from the emr and pcc objects while keeping the rest of the object intact. The issue with the way I'm implementing this change is I am trying to use the delete operator which works but I have to delete 10 items from the emr and pcc data separately so my code does not look good. Can anyone show me a better way to go about doing this?

this is what my codes looking like right now:

const pcc = result.pcc;
const emr = result.emr;

delete pcc["MedicalPractices"];
delete pcc["CovidZone"];
delete pcc["picturePath"];
delete pcc["DoseSpotID"];
delete pcc["consentStatus"];
delete pcc["consentStatusLastUpdate"];
delete pcc["consentStatusUpdatedBy"];
delete pcc["consentStatusChangeReason"];
delete pcc["syncStatus"];
delete emr["MedicalPractices"];
delete emr["CovidZone"];
delete emr["picturePath"];
delete emr["DoseSpotID"];
delete emr["consentStatus"];
delete emr["consentStatusLastUpdate"];
delete emr["consentStatusUpdatedBy"];
delete emr["consentStatusChangeReason"];
delete emr["syncStatus"];

console.log(pcc);
this.setState({ pccData: pcc });
this.setState({ emrData: emr });
1
  • It might be worth determining whether the properties you're keeping are fewer than those your are deleting and mapping those to new objects. Better yet would be querying only the props you need from the API if that is how you are getting them. Commented Jun 28, 2021 at 21:09

2 Answers 2

2

I'd create two lists, one of the objects you want to delete from and one of the properties you want to delete and iterate over them in a nested loop:

const objects = ["pcc", "emr"];
const props = ["MedicalPractices", "CovidZone", "picturePath", "DoseSpotID", "consentStatus", "consentStatusLastUpdate", "consentStatusUpdatedBy", "consentStatusChangeReason", "syncStatus"];
objects.forEach(o => {
    props.forEach(p => 
        delete result[o][p];
    });
});
Sign up to request clarification or add additional context in comments.

2 Comments

Hey thanks this is super helpful. I understand what youre doing here. Im just thrown off by the delete statement what is result?
@CourtneyJ objects may not have been the best name, TBH... objects are the names of the properties of results you want to handle - "pcc" and "emr". props are the names of the properties of each of those you want to delete. I then have nested loop over both of them, and delete the properties one by one. So the first iteration deletes result['pcc']['MedicalPractices']. The second iteration deletes result['pcc']['CovidZone'] and so on
1

Creating an array of items you want to delete and then just looping for each item. Finally deleting those mentioned in arrays !

Deleting separately

let delete_items_pcc = ["MedicalPractices", "CovidZone", "picturePath" ....]
let delete_items_emr = ["MedicalPractices", "CovidZone", "picturePath" ....]
delete_items_pcc.forEach(item => delete pcc[item])
delete_items_emr.forEach(item => delete emr[item])

Deleting simultaneously

let delete_items = ["MedicalPractices", "CovidZone", "picturePath" ....]
delete_items.forEach(item => {
  delete emr[item]
  delete pcc[item]
})

2 Comments

It looks like they're the same list of properties, so may not need two item arrays.
@Barmar k didn't saw that !

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.