Created
April 10, 2023 11:11
-
-
Save nakasyou/ff1083c9a476ff3eea47768ca9b3c9d4 to your computer and use it in GitHub Desktop.
JS/TSで任意のオブジェクトに動的にアクセスする関数
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
function objAcsess(obj: object, keys: Array<string|number>, value:any) :any{ | |
let target = obj; | |
keys.slice(0,-1).forEach(key=>{ | |
target = target[key]; | |
}); | |
if(value){ | |
target[keys.at(-1)] = value; | |
} | |
return target[keys.at(-1)]; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment