Example 1 — Merge Two Objects (Shallow)
Official style: modify the first object; nested keys are replaced.
var object1 = {
apple: 0,
banana: { weight: 52, price: 100 },
cherry: 97
};
var object2 = {
banana: { price: 200 },
durian: 100
};
$.extend(object1, object2);
console.log(JSON.stringify(object1));
// {"apple":0,"banana":{"price":200},"cherry":97,"durian":100} How It Works
- Target.
object1receives new properties in place. - Replace.
bananabecomes{ price: 200 }—weightis lost. - Add.
durian: 100is copied onto the target.

