Files
flowsint/test.js
2025-09-04 14:32:17 +02:00

18 lines
707 B
JavaScript

export function deepObjectDiff(obj1, obj2) {
let diffObject = {}
// We want object 2 to be compared against object 1
if (typeof obj1 != "object" || typeof obj2 != "object") throw Error("Items to compare mustr be objects.")
// We map over the obj2 key:value duos
Object.entries(obj2).forEach(([key, value]) => {
// We check for additional keys
if (!obj1.hasOwnProperty(key))
diffObject = { ...diffObject, [key]: { value, new: true } }
else {
diffObject = { ...diffObject, [key]: { value, new: false, oldValue: obj1[key] ?? null, newValue: obj2[key] ?? null, identical: obj2[key] === obj1[key] } }
}
})
return diffObject
}