Skip to content

Instantly share code, notes, and snippets.

@nakasyou
Created April 10, 2023 11:11
Show Gist options
  • Save nakasyou/ff1083c9a476ff3eea47768ca9b3c9d4 to your computer and use it in GitHub Desktop.
Save nakasyou/ff1083c9a476ff3eea47768ca9b3c9d4 to your computer and use it in GitHub Desktop.
JS/TSで任意のオブジェクトに動的にアクセスする関数
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