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

问题描述 投票:0回答:1

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

let orders = [ 
    { 
        "email": "[email protected]",
        "field_1": null,
        "field_2": "This has some value",
        "field_3": null,
        "field_4": null,
        "field_5": 1
    }, 
    { 
        "email": "[email protected]",
        "field_1": null,
        "field_2": "This has some value",
        "field_3": null,
        "field_4": null
        "field_5": 2
    },
    { 
        "email": "[email protected]",
        "field_1": null,
        "field_2": "This has some other value",
        "field_3": null,
        "field_4": null,
        "field_5": 250
    },

    ];

let contacts = [ 
    { 
        "email": "[email protected]",
        "field_1": "Name1",
        "field_2": null,
        "field_3": "Address 1"
        "field_4": true
        "field_5": 1
    }, 
    { 
        "email": "[email protected]",
        "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": "[email protected]",
        "field_1": "Name1",
        "field_2": "This has some value",
        "field_3": "Address 1",
        "field_4": true,
        "field_5": 1
    }, 
    { 
        "email": "[email protected]",
        "field_1": "Name2",
        "field_2": "This has some value",
        "field_3": "Address 2",
        "field_4": true
        "field_5": 2
    },
    { 
        "email": "[email protected]",
        "field_1": "Name1",
        "field_2": "This has some other value",
        "field_3": "Address 1",
        "field_4": true,
        "field_5": 250
    },

];
javascript arrays json
1个回答
2
投票

[Map通过email联系]

    处理订单,查找映射器中是否存在使用相同电子邮件的联系人。
  • 遍历当前元素的键并检查该值是否为空
  • 如果为空,则替换为mapper中的值,否则保持原状
  • let orders = [{"email": "[email protected]","field_1": null,"field_2": "This has some value","field_3": null,"field_4": null,"field_5": 1},{"email": "[email protected]","field_1": null,"field_2": "This has some value","field_3": null,"field_4": null, "field_5": 2},{"email": "[email protected]","field_1": null,"field_2": "This has some other value","field_3": null,"field_4": null,"field_5": 250},]; let contacts = [{"email": "[email protected]","field_1": "Name1","field_2": null,"field_3": "Address 1","field_4": true, "field_5": 1},{"email": "[email protected]","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)
  • © www.soinside.com 2019 - 2024. All rights reserved.