Doe
Doe

Reputation: 193

Merge specific prop of objects into one using es6?

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.

Upvotes: 1

Views: 114

Answers (5)

georg
georg

Reputation: 214949

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);

Upvotes: 1

Nina Scholz
Nina Scholz

Reputation: 386560

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);

Upvotes: 1

Keegan Brown
Keegan Brown

Reputation: 535

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

Upvotes: 1

Jonas Wilms
Jonas Wilms

Reputation: 138257

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

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

Upvotes: 2

Varinder
Varinder

Reputation: 2664

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

Upvotes: 0

Related Questions