简体   繁体   中英

Associate two keys in an object JavaScript

So I have this object that has two keys, clinics and invitations . I want to associate the clinics with the invitations taking into account the clinicId :

const upcomingClinics = {
  "clinics": {
    "a0CW000000271LuMAI": {
      "id": "a0CW000000271LuMAI",
      "contact": {
        "name": null,
        "phone": null,
        "email": null
      },
      "shifts": {
        "teamLeads": 1,
        "healthTechs": 1
      }
    },
    "a0CW00000026gikMAA": {
      "id": "a0CW00000026gikMAA",
      "contact": {
        "name": null,
        "phone": null,
        "email": null
      },
      "shifts": {
        "teamLeads": 1,
        "healthTechs": 4
      }
    }
  },
  "invitations": {
    "56392": {
      "id": "56392",
      "clinicId": "a0CW00000026gikMAA"
    },
    "56393": {
      "id": "56393",
      "clinicId": "a0CW00000026gikMAA"
    },
    "56402": {
      "id": "56402",
      "clinicId": "a0CW00000026gikMAA"
    },
    "56427": {
      "id": "56427",
      "clinicId": "a0CW000000271LuMAI"
    },
    "56428": {
      "id": "56428",
      "clinicId": "a0CW000000271LuMAI"
    }
  }
}

The keys of the clinics object always match the IDs . Basically I want this object to look like this, because they have in common the same clinicId , how can I do this? Inserting a new key invitations to the clinics object?:

const upcomingClinics = {
  "clinics": {
    "a0CW000000271LuMAI": {
      "id": "a0CW000000271LuMAI",
      "contact": {
        "name": null,
        "phone": null,
        "email": null
      },
      "shifts": {
        "teamLeads": 1,
        "healthTechs": 1
      }
      "invitations": {
        "56427": {
          "id": "56427",
          "clinicId": "a0CW000000271LuMAI"
        },
        "56428": {
          "id": "56428",
          "clinicId": "a0CW000000271LuMAI"
        }
      }
    },
    "a0CW00000026gikMAA": {
      "id": "a0CW00000026gikMAA",
      "contact": {
        "name": null,
        "phone": null,
        "email": null
      },
      "shifts": {
        "teamLeads": 1,
        "healthTechs": 4
      }
      "invitations": {
        "56392": {
          "id": "56392",
          "clinicId": "a0CW00000026gikMAA"
        },
        "56393": {
          "id": "56393",
          "clinicId": "a0CW00000026gikMAA"
        },
        "56402": {
          "id": "56402",
          "clinicId": "a0CW00000026gikMAA"
        },
      }
    }
  },
}

Thanks!

i think this is what you are looking for https://jsfiddle.net/q4rt6zad/10/

Object.getOwnPropertyNames(upcomingClinics.clinics).forEach((clinicId) => {
  upcomingClinics.clinics[clinicId].invitations = {};
  Object.getOwnPropertyNames(upcomingClinics.invitations).forEach((id) => {
    const invite = upcomingClinics.invitations[id];
    if (invite.clinicId === clinicId) {
      upcomingClinics.clinics[clinicId].invitations[id] = invite;
    }
  });
});
delete upcomingClinics.invitations;

Just loop the invitations object, and for each invitation check if its clinic is already included in upcomingClinics object, if so then just add this invitation to its invitations object, otherwise, create a new clinic record in upcomingClinics then insert the current invitation to its invitations object:

let result = Object.keys(upcomingClinics.invitations).reduce(function(result, invitationId) {   // for each invitationId in upcomingClinics.invitations object
    let invitation = upcomingClinics.invitations[invitationId];                    // get the current invitation object
    let clinicId = invitation.clinicId;                                            // get its clinicId
    if(!result[clinicId]) {                                                        // if there is no record of this clinic in the result object
        result[clinicId] = Object.create(upcomingClinics.clinics[clinicId]);       // create one by cloning the clinic object from upcomingClinics.clinics
        result[clinicId].invitations = {};                                         // create an object that will hold its invitations
    }
    result[clinicId].invitations[invitationId] = invitation;                       // add the current invitation to its corresponding clinic object
    return result;
}, {});

Example:

 const upcomingClinics = {"clinics":{"a0CW000000271LuMAI":{"id":"a0CW000000271LuMAI","contact":{"name":null,"phone":null,"email":null},"shifts":{"teamLeads":1,"healthTechs":1}},"a0CW00000026gikMAA":{"id":"a0CW00000026gikMAA","contact":{"name":null,"phone":null,"email":null},"shifts":{"teamLeads":1,"healthTechs":4}}},"invitations":{"56392":{"id":"56392","clinicId":"a0CW00000026gikMAA"},"56393":{"id":"56393","clinicId":"a0CW00000026gikMAA"},"56402":{"id":"56402","clinicId":"a0CW00000026gikMAA"},"56427":{"id":"56427","clinicId":"a0CW000000271LuMAI"},"56428":{"id":"56428","clinicId":"a0CW000000271LuMAI"}}}; let result = Object.keys(upcomingClinics.invitations).reduce(function(result, invitationId) { let invitation = upcomingClinics.invitations[invitationId]; let clinicId = invitation.clinicId; if(!result[clinicId]) { result[clinicId] = Object.create(upcomingClinics.clinics[clinicId]); result[clinicId].invitations = {}; } result[clinicId].invitations[invitationId] = invitation; return result; }, {}); console.log(result); 

const clinics = {};

for (let clinicId in upcomingClinics.clinics) {
  clinics[clinicId] = upcomingClinics.clinics[clinicId];
  clinics[clinicId].invitations = {};
  for (let invitId in upcomingClinics.invitations) {
    const invitation = upcomingClinics.invitations[invitId];
    if (invitation.clinicId === clinicId) {
      clinics[clinicId].invitations[invitId] = invitation;
    }
  }
}

https://jsfiddle.net/bg6srahq/

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