Lodash merge including undefined values
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.
Lodash docs for _.merge() says:
"Recursively merges own enumerable properties of the source object(s), that don't resolve to undefined into the destination object."
Is there another function that can do this, or can it be easily overwritten?
EDIT A:
Sample input:
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"
}
Expected Output
B = {
name: "Bob Smith",
job: "Racing Driver",
address: undefined,
phone: "0800 800 80"
}
EDIT B:
Just to confirm, it needs to be a "deep" merge. object may contain nested objects.
最简单的方法是使用第三个包来实现这个https://github.com/unclechu/node-deep-extend,其目标只有深度合并,没有别的。
_.assign
/ _.extend
会这样做:
_.assign(B, A);
Check this out. It is a small gist containing a deep extend
lodash method which you can use within your own application. This will also allow you to get the functionality you need without adding another dependency in your app (since you're already using lodash )
上一篇: wordpress精选来自外部网址的图片,无需下载
下一篇: Lodash合并,包括未定义的值