Created
September 8, 2020 20:49
-
-
Save BiosBoy/63b872efc8a5e3f9034aa047410018a7 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
| const strToObj = (arg) => { | |
| const arr = arg.push ? arg : arg.split('.') | |
| const obj = {} | |
| const _recursiveParsing = (arrBuff, objBuff) => { | |
| if (arrBuff.length === 0) { | |
| return obj | |
| } | |
| const keyToInsert = arrBuff[0] | |
| objBuff[keyToInsert] = arrBuff && arrBuff[1] ? {} : null | |
| return _recursiveParsing(arrBuff.slice(1, arr.length), objBuff[keyToInsert]) | |
| } | |
| return _recursiveParsing(arr, obj) | |
| } | |
| const result = strToObj('a.b.c.d') | |
| console.log(JSON.stringify(result)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment