TehShrike / deepmerge

A library for deep (recursive) merging of Javascript objects
MIT License
2.75k stars 216 forks source link

Add a way to merge into existing target object, just like Object.assign #261

Open jihu opened 9 months ago

jihu commented 9 months ago

My use case is that I already have an object, and I want to merge another object into it, without getting a new object. Just like Object.assign works. Maybe you could add a option for this, called 'treatFirstObjectAsTarget'.

Example:

const sourceObject = { a: 1, b: 2, c: 3 };
const targetObject = { x: 10, y: 20, z: 30 };
deepMerge(targetObject, sourceObject, { treatFirstObjectAsTarget: true });
console.info(targetObject); // Should print: { x: 10, y: 20, z: 30, a: 1, b: 2, c: 3 };