1

There is a small problem

I need to compare two objects, and based on the same keys in these objects pass values from second object to the original object.

Original object

{
   content: "Old Value",
   height: 200,
   id: "15a2d286-d123-1cf0-4e40-b043b055a49f",
   type: "location",
   units: "some unit value",
   width: 200,
}

Second object

{
   units: "some unit value", 
   content: "New Value"
}

Keys is not necessary to check for compliance

The output will be something like that

{
   content: "New Value", // changed value
   height: 200,
   id: "15a2d286-d123-1cf0-4e40-b043b055a49f",
   type: "location",
   units: "some unit value", // changed value
   width: 200,
}

Is there any elegant solution to this problem with Lodash or Underscore?

bast
  • 90
  • 8

1 Answers1

0

this will do the trick (using lodash)

var source = {
  content: "Old Value",
  height: 200,
  id: "15a2d286-d123-1cf0-4e40-b043b055a49f",
  type: "location",
  units: "some unit value",
  width: 200,
},
source2 = {
  units: "some unit value", 
  content: "New Value"
}
_.assignIn(source , source2 );
Zamboney
  • 2,002
  • 12
  • 22
  • if i may ask, what is the difference between _.assignIn and _.merge ? for , _.merge(source,source2) seems to do the same thing ! – semuzaboi Apr 14 '16 at 11:08