Merge specific prop of objects into one using es6?

2019-08-04 01:15发布

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.

5条回答
手持菜刀,她持情操
2楼-- · 2019-08-04 01:55

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,
}
查看更多
甜甜的少女心
3楼-- · 2019-08-04 01:57

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 };
查看更多
啃猪蹄的小仙女
4楼-- · 2019-08-04 02:00

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

查看更多
Emotional °昔
5楼-- · 2019-08-04 02:07
 const result = (({name, type}, {age, dob}) => ({name, type, age, dob}))(P1, P2);

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

查看更多
ら.Afraid
6楼-- · 2019-08-04 02:11

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);
查看更多
登录 后发表回答