Lodash合并包括未定义的值 [英] Lodash merge including undefined values
问题描述
我正在尝试使用Lodash将对象A合并到对象B中,但我遇到的问题是对象A有一些未定义的值,我希望将它们复制到对象B。
I'm trying to use Lodash to merge object A into object B, but the trouble I am having is that object A has some undefined values and I want these to be copied over to object B.
_.merge()的Lodash文档说:
Lodash docs for _.merge() says:
递归合并源对象的自身可枚举属性,不解析为未定义到目标对象。
是否有其他功能可以执行此操作,还是可以轻松覆盖?
Is there another function that can do this, or can it be easily overwritten?
编辑A:
示例输入:
A = {
name: "Bob Smith",
job: "Racing Driver",
address: undefined
}
B = {
name: "Bob Smith",
job: "Web Developer",
address: "1 Regent Street, London",
phone: "0800 800 80"
}
预期输出
B = {
name: "Bob Smith",
job: "Racing Driver",
address: undefined,
phone: "0800 800 80"
}
编辑B:
为了确认,它需要是一个深度合并。 object可能包含嵌套对象。
Just to confirm, it needs to be a "deep" merge. object may contain nested objects.
推荐答案
最简单的方法是使用第3个包 https://github.com/unclechu/node-deep-extend 哪个目标只是深度合并而没有别的。
Easiest would be to use 3rd package for this https://github.com/unclechu/node-deep-extend which goal is only deep merging and nothing else.
这篇关于Lodash合并包括未定义的值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!