I'm working on a NodeJS project (using @babel/preset-env
) and need to figure something out:
If I have two objects, one of them identical to the other but possibly minus some fields (depends on input object), how do I edit the "larger" one such that its fields are changed to the values of the matching ones from the smaller object?
That's probably worded badly, so here's an example...say I have these two objects:
const obj1 = {
a: 1,
b: 2,
c: 3,
d: 4
}
const obj2 {
b: 'hello',
d: 'world'
}
What I'm wanting to do is modify obj1
in this instance such that the values of obj1.b
and obj1.d
get updated with the values from obj2
.
Is there a way to edit the values directly, or should I just create a copy of obj1
and set values depending on whether or not they exist in obj2
?