r/learnjavascript • • Feb 16 '21

Javascript Object Merge

Post image
183 Upvotes

28 comments sorted by

View all comments

4

u/Crypticsafe5 Feb 17 '21

My only issue with this snippet is that it allows mutability of the schema that's passed in potentially causing data loss or data integrity issues. I'd definitely add in a check something along the lines of:

if (
    typeof a[key] !== typeof b[key]
    || Array.isArray(a[key]) !== Array.isArray(b[key])
) throw Error('Non-matching schema');