Last active
June 5, 2024 11:03
-
-
Save ArtemSites/d7f8f8b8e01beb69b8f13f02e5ae99c4 to your computer and use it in GitHub Desktop.
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
// @ts-check | |
/** | |
* @export | |
* @param {object} targetObj | |
* @param {array} arPathAndValue | |
* | |
* @example objectUpdateNested(menuChoicesLS, [ this.groupId, this.programId, weekday, type, String(alt) ]) | |
*/ | |
export function objectUpdateNested(targetObj, arPathAndValue) { | |
let current = targetObj; | |
const value = arPathAndValue.pop(); | |
for (let i = 0; i < arPathAndValue.length - 1; i++) { | |
if (typeof current[arPathAndValue[i]] !== 'object' || current[arPathAndValue[i]] === null) { | |
current[arPathAndValue[i]] = {}; | |
} | |
current = current[arPathAndValue[i]]; | |
} | |
current[arPathAndValue[arPathAndValue.length - 1]] = value; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment