通过ID合并2个对象

时间:2018-08-24 15:57:46

标签: javascript object

我想用Java脚本按ID合并两个对象,但是我不知道如何

var personData = {
    person1 : {
        uid: 112233,
        city: "esfahan",
        postalCode: 2345672345,
        phoneNumber: "03111234234",
        position: "ui designer"
    }}

var additionalPersonData = {
    person12: {
        uid: 112233,
        firstName: "reza",
        lastName: "hosseini"
    }}
  

因此输出应如下所示:

result = [
 {
  uid: 112233,
  firstName: "reza",
  lastName: "hosseini"
  city: "isfahan",
  postalCode: 2345672345,
  phoneNumber: "03111234234",
  position: "ui designer"
}]

1 个答案:

答案 0 :(得分:0)

使用Object.assign():

    var personData = {
    person1: {
        uid: 112233,
        city: "esfahan",
        postalCode: 2345672345,
        phoneNumber: "03111234234",
        position: "ui designer"
    }
    };

    var additionalPersonData = {
    person12: {
        uid: 112233,
        firstName: "reza",
        lastName: "hosseini"
    }
    };

    var result = [];

    result.push(Object.assign(
    {},
    personData.person1,
    additionalPersonData.person12
    ));

    console.log(result);