1.0.7 • Published 5 years ago
object-union v1.0.7
Object Union
Merge two or more objects non-destructively.
Rules
- Merge two or more objects in such a way that if a property existed in previous object but absent in new object then that property will be preserved.
- If a property is there in previous object and also there in next object then resultant object will have value of the latest object.
- Recursively merge nested objects
Usage
const union = require("object-union");
const obj1 = { a: "", b: "BB" };
const obj2 = { a: "AA", b: "" };
const res = union(obj1, obj2);
console.log(res)
Output:
{ a: "AA", b: "BB" }
Merging nested objects
const obj1 = {
prop: { a: 1, b: 2 },
};
const obj2 = {
prop: { a: 2, c: 3 },
};
const res = union(obj1, obj2);
console.log(res)
Output:
{ prop: { a: 2, b: 2, c: 3 } }