1

This is by far the most common question asked in SO, however, all in all, the questions asked refers to merging two whole objects.

In my case, it's quite different.

Suppose I'm given:

const P1 = {
  "name"          : "Person1",
  "profession"    : "Student",
  "gender"        : "Male",
  "type"          : "Patient",
}

const E1 = {
  "age"           : "30",
  "dob"           : "20/12/1970",
  "address"       : "122 Harrow Street",
  "contactNumber" : "07473033312",
}

and I want to merge these two to give me the following:

const Result = {
  "name"          : "Person1",
  "type"          : "Patient",
  "age"           : "30",
  "dob"           : "20/12/1970",
}

The issue is, I don't want to merge two whole projects. I want to merge specific props without looping.

Currently, we can achieve the merge by using the spread like so:

const data = [...P1, ...E1];.

But this merges both, which I don't want.

Jonas Wilms
  • 132,000
  • 20
  • 149
  • 151
Doe
  • 193
  • 4
  • 14
  • where is your specific problem? – Nina Scholz Feb 14 '18 at 20:30
  • I've updated my question @NinaScholz – Doe Feb 14 '18 at 20:33
  • @doe for future reference, once your original question has been answered (it was), do not edit it to ask a different question rendering the original answers no longer valid. Just accept and ask a new question. – Jason Cust Feb 14 '18 at 21:02
  • Sorry guys. My fault. Would my question be closed due to the similarity? @JasonCust – Doe Feb 14 '18 at 21:04
  • @doe You should give it a try yourself first and when/if you get stuck then post and include what you have tried and where you are stuck. – Jason Cust Feb 15 '18 at 00:38

5 Answers5

2
 const result = (({name, type}, {age, dob}) => ({name, type, age, dob}))(P1, P2);

Just partially destruct P1 and P2 and build up a new object.

Jonas Wilms
  • 132,000
  • 20
  • 149
  • 151
  • Entirely my fault. Your answer is acceptable. However, how would this be applied if we had an array of objects instead of just P1 and E1? – Doe Feb 14 '18 at 20:48
  • @doe you did not update, you changed it completely. Why havent you given that earlier?? Ok, i think it would be the best to simply ask a new question with your new code. – Jonas Wilms Feb 14 '18 at 21:01
  • Apologies, entirely my fault. I have forgotten that I had nested objects instead of single. Apologies again. – Doe Feb 14 '18 at 21:02
1

Since you're looking for an ES6 way, I'd say using deconstruction is the way to go:

const P1 = {
    "name"          : "Person1",
    "profession"    : "Student",
    "gender"        : "Male",
    "type"          : "Patient",
}

const E1 = {
    "age"           : "30",
    "dob"           : "20/12/1970",
    "address"       : "122 Harrow Street",
    "contactNumber" : "07473033312",
}

const { name, type } = P1;
const { age, dob } = E2;
const Result = { name, type, age, dob };
Keegan Brown
  • 515
  • 3
  • 5
1

You could use a complete dynamic approach by using an array for the wanted properties and another for the objects.

Then build a new object out of the found objects.

var p1 = { name: "Person1", profession:"Student", gender:"Male", type:"Patient" },
    e1 = { age: "30", dob:"20/12/1970", address:"122 Harrow Street", contactNumber:"07473033312" },
    keys = ['name', 'profession', 'age', 'dob'],
    objects = [p1, e1],
    merged = Object.assign(...keys.map(k => ({ [k]: objects.find(o => k in o)[k] })));

console.log(merged);
Nina Scholz
  • 376,160
  • 25
  • 347
  • 392
1

I guess your best bet is to unpack and then pack it again:

let {name, type, age, dob} = {...P1, ...E1};
result = {name, type, age, dob}

This is silly, but that's all we've got in ES6.

An alternative would be a function like lodash's _.pick.

If you have an array of source objects, replace {...P1, ...E1} with spreaded Object.assign:

let {name, type, age, dob} = Object.assign({}, ...a);
georg
  • 211,518
  • 52
  • 313
  • 390
0

If you're aware of what properties the merged object should have (in your case name, type, age, dob)

How about defining them like so:

const P1 = {
    "name"          : "Person1",
    "profession"    : "Student",
    "gender"        : "Male",
    "type"          : "Patient",
}

const E1 = {
    "age"           : "30",
    "dob"           : "20/12/1970",
    "address"       : "122 Harrow Street",
    "contactNumber" : "07473033312",
}

const Result = {
    "name"          : P1.name || E1.name,
    "type"          : P1.type || E1.type,
    "age"           : P1.age || E1.age,
    "dob"           : P1.dob || E1.dob,
}
Varinder
  • 2,634
  • 1
  • 17
  • 20