Created
February 19, 2021 08:49
-
-
Save ilhamgusti/83bed418013bf0fd2eae8023ed47e9bb to your computer and use it in GitHub Desktop.
javascript string dot notation into object reference
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 index(obj,is, value) { | |
if (typeof is == 'string') | |
return index(obj,is.split('.'), value); | |
else if (is.length==1 && value!==undefined) | |
return obj[is[0]] = value; | |
else if (is.length==0) | |
return obj; | |
else | |
return index(obj[is[0]],is.slice(1), value); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment