Last active
September 10, 2023 05:17
-
-
Save amekusa/abb364400c0e98cba99aec0a0d9c519b to your computer and use it in GitHub Desktop.
Deep Merge.js
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* 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