1

Suppose we are given the following:

const Patients = {
  P1: {
    "name": "Person1",
    "profession": "Student",
    "gender": "Male",
    "type": "Patient",
    "Doctors": {...}
  },
  P2: {
    "name": "Person2",
    "profession": "Student",
    "gender": "Male",
    "type": "Patient",
    "Doctors": {...}
  }
}

const Doctors = {
  D1: {
    "name": "Doctor1",
    "profession": "Dr",
    "gender": "Male",
    "type": "Doctor",
    "Patients": {...}
  }
}

How can we merge the two objects (Patients & Doctors) as One object so that the result is as follows:

const Result = {
  "name": "Doctor1",
  "profession": "Dr",
  "Patients": {...},
  P1: {
    "Doctors": {...}
  },
  P2: {
    "Doctors": {...}
  }
}

As far as I know, I could use destruct on both objects to partially destruct and form a new object. But this makes it harder to obtain the nested object (i.e. "Doctors": {...} within P1 and P2.

For example:

let result = (({
      name,
      profession,
      Patients
    }, { /* Im not sue what to do here */ }) => ({
      Patients,
      /* Im not sue what to do here */ ))(Doctor, Object.values(Patients));
Patrick Roberts
  • 49,224
  • 10
  • 102
  • 153
Doe
  • 193
  • 4
  • 14
  • 2
    Didn't you just ask [this question](https://stackoverflow.com/questions/48795653/merge-specific-prop-of-objects-into-one-using-es6)? – Icepickle Feb 14 '18 at 22:24
  • Yes I did, but I had changed my previous question, but the majority said to ask a new question. Please check the previous question's comments. Basically, the difference now is that we have to take care of props of nested objects @Icepickle – Doe Feb 14 '18 at 22:25
  • I don't see btw what a merge would do for you, you seem to want to create a linked structure between patients and doctors? – Icepickle Feb 14 '18 at 22:27
  • So the issue I'm facing is that in reality each of these objects is very huge, and I don't want to pass everything around. I only would want few props from both objects and use that. @Icepickle – Doe Feb 14 '18 at 22:28
  • 1
    I don't get the problem. You don't have to do anything about those nested objects, do you? Your inputs and outputs are kinda confusing due to the `{...}` ellipsis and the missing comma. – Bergi Feb 14 '18 at 22:29
  • I am assuming that doctors and patients would then be used as a lookup table (probably based on a key like name) in case you have to find back data? How do you mean btw to pass a huge object around? If it's a reference then it doesn't cost you anything (or at least not nearly as much as you think it would)? At that time it would really depend on what you do in the rest of your code – Icepickle Feb 14 '18 at 22:31
  • @Icepickle I think your right, I usually have a reference to those objects. which I try to lookup the nested object somewhere else. – Doe Feb 14 '18 at 22:34
  • Could it be this is an [xy - problem](https://meta.stackexchange.com/questions/66377/what-is-the-xy-problem/66378#66378)? I have a hard time understanding why you want to pull this object apart. Then why do you construct it in the first place? Actually, following your question history, to me it seems that your json is your "db" or at least that is what you are using it for, why do you want to do it in such a hard way? What is server side, what would be wrong with storing data, and retrieving per entity / screen? – Icepickle Feb 14 '18 at 22:41
  • @Icepickle That's exactly the problem. I have fallen into xy-problem haha! I think I've already asked too many questions, and I feel I have annoyed the SO community. I'm so sorry! – Doe Feb 15 '18 at 13:05
  • @Doe I get the sarcasm, I just wanted to understand better why you would have such a structure but then need to go through the trouble of separating it all. Asking questions is not a problem at all, and they will be replied to ;) – Icepickle Feb 15 '18 at 13:58

1 Answers1

2

Use spread syntax ... and reduce function.

const Patients = {  P1: {    "name": "Person1",    "profession": "Student",    "gender": "Male",    "type": "Patient",    "Doctors": {}  },  P2: {    "name": "Person2",    "profession": "Student",    "gender": "Male",    "type": "Patient",    "Doctors": {}  }}
const Doctors = {  D1: {    "name": "Doctor1",    "profession": "Dr",    "gender": "Male",    "type": "Doctor",    "Patients": {}  }}

var result = { ...Doctors.D1,
  ...Object.keys(Patients).reduce((a, k) => {
    a[k] = {
      "Doctors": Patients[k].Doctors
    };
    return a;
  }, {})
};

delete result.gender;
delete result.type;

console.log(result);
.as-console-wrapper {
  max-height: 100% !important
}
Patrick Roberts
  • 49,224
  • 10
  • 102
  • 153
Ele
  • 33,468
  • 7
  • 37
  • 75