I am trying to match update an object’s property based on another object’s property. The property names must match. A very simple example of what I am trying to achieve would look like this:
Given two objects:
var obj1 = {
one: "1",
two: "2",
three: {
threeDotOne: "3.1",
threeDotTwo: "3.2",
threeDotAny: "3.3"
}
}
var obj2 = {threeDotAny: "3.4"}
updateObjectOneProperty(obj1, obj2)
and the result would look like this:
var obj1 = {
one: "1",
two: "2",
three: {
threeDotOne: "3.1",
threeDotTwo: "3.2",
threeDotAny: "3.4"
}
}
Here I am assuming that there will only be ONE property with such a name in obj1, which is fine.
Does anyone have any idea on how to do this?
jQuery’s
$.extendcan do it:http://jsfiddle.net/vjCvM/
If you wanted to be able to do it without specifying
obj1.three,threewould need to be a property ofobj2otherwise there’s no way of knowing where to put it.