如果一个键值相同,则需要合并两个对象数组

时间:2019-10-04 02:21:39

标签: javascript arrays json

我有两个数据字段-我需要合并的对象数组

let orders = [ 
    { 
        "email": "name1@gmail.com",
        "field_1": null,
        "field_2": "This has some value",
        "field_3": null,
        "field_4": null,
        "field_5": 1
    }, 
    { 
        "email": "name2@gmail.com",
        "field_1": null,
        "field_2": "This has some value",
        "field_3": null,
        "field_4": null
        "field_5": 2
    },
    { 
        "email": "name1@gmail.com",
        "field_1": null,
        "field_2": "This has some other value",
        "field_3": null,
        "field_4": null,
        "field_5": 250
    },

    ];

let contacts = [ 
    { 
        "email": "name1@gmail.com",
        "field_1": "Name1",
        "field_2": null,
        "field_3": "Address 1"
        "field_4": true
        "field_5": 1
    }, 
    { 
        "email": "name2@gmail.com",
        "field_1": "Name2",
        "field_2": null,
        "field_3": "Address 2"
        "field_4": true
        "field_5": 2
    }
];

我需要的是将两个数组合并为一个,其中它们仅通过唯一字段(即电子邮件)连接,并覆盖所有这些空字段。我需要通过电子邮件从通讯录数组中填充的第一个对象空字段数组。

到目前为止,我尝试过合并两个数组,但是由于所有字段都存在于两个对象中,因此不会覆盖它们。因此,我尝试从第一个数组中删除null元素,因为它们将从第二个数组中添加,但是第二个数组中的大多数null字段将覆盖第一个数组中的数据。

我也可以删除第二个数组中的空项目,然后合并它们,但是我需要显示所有字段。

还有更好的方法吗?

data1.forEach(element => {
  Object.keys(element).forEach(key => {
    if(element[key] === null) return delete element[key]

    return element[key]
  })
})

const newArr = _.merge(data1, data2);

预期的结果数组将是:

let orders = [ 
    { 
        "email": "name1@gmail.com",
        "field_1": "Name1",
        "field_2": "This has some value",
        "field_3": "Address 1",
        "field_4": true,
        "field_5": 1
    }, 
    { 
        "email": "name2@gmail.com",
        "field_1": "Name2",
        "field_2": "This has some value",
        "field_3": "Address 2",
        "field_4": true
        "field_5": 2
    },
    { 
        "email": "name1@gmail.com",
        "field_1": "Name1",
        "field_2": "This has some other value",
        "field_3": "Address 1",
        "field_4": true,
        "field_5": 250
    },

];

1 个答案:

答案 0 :(得分:2)

您可以使用Mapmap

  • Mapemail的联系
  • 查看订单,查找映射器中是否存在使用相同电子邮件的联系人。
  • 遍历当前元素的键并检查该值是否为空
  • 如果它为空,则用mapper中的值替换,否则保持原样

let orders = [{"email": "name1@gmail.com","field_1": null,"field_2": "This has some value","field_3": null,"field_4": null,"field_5": 1},{"email": "name2@gmail.com","field_1": null,"field_2": "This has some value","field_3": null,"field_4": null, "field_5": 2},{"email": "name1@gmail.com","field_1": null,"field_2": "This has some other value","field_3": null,"field_4": null,"field_5": 250},];
let contacts = [{"email": "name1@gmail.com","field_1": "Name1","field_2": null,"field_3": "Address 1","field_4": true, "field_5": 1},{"email": "name2@gmail.com","field_1": "Name2","field_2": null,"field_3": "Address 2","field_4": true ,"field_5": 2}];

let mapper = new Map(contacts.map(v => [v.email, v]))

let final = orders.map(v => {
  let contact = mapper.get(v.email)
  if (contact) {
    for (let key in v) {
      if (v[key] === null) {
        v[key] = contact[key]
      }
    }
  }
  return v
})

console.log(final)