Lodash合并包括未定义的值 [英] Lodash merge including undefined values

查看:83
本文介绍了Lodash合并包括未定义的值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆