Skip to content

Instantly share code, notes, and snippets.

@amekusa
Last active September 10, 2023 05:17
Show Gist options
  • Save amekusa/abb364400c0e98cba99aec0a0d9c519b to your computer and use it in GitHub Desktop.
Save amekusa/abb364400c0e98cba99aec0a0d9c519b to your computer and use it in GitHub Desktop.
Deep Merge.js
/**
* Merges the 2nd object into the 1st object recursively (deep-merge). The 1st object will be modified.
* @param {object} x - The 1st object
* @param {object} y - The 2nd object
* @param {number} recurse=8 - Recurstion limit. Negative number means unlimited
* @return {object} The 1st object
* @author amekusa
*/
function merge(x, y, recurse = 8) {
if (recurse && x && y && typeof x == 'object' && typeof y == 'object' && !Array.isArray(x) && !Array.isArray(y)) {
for (let key in y) x[key] = merge(x[key], y[key], recurse - 1);
} else return y;
return x;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment