Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Lodash merge including undefined values

Tags:

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.

like image 516
jonhobbs Avatar asked Mar 22 '14 17:03

jonhobbs


1 Answers

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.

like image 105
Eugene Nagorny Avatar answered Sep 18 '22 06:09

Eugene Nagorny