Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Select an option

  • Save BiosBoy/63b872efc8a5e3f9034aa047410018a7 to your computer and use it in GitHub Desktop.

Select an option

Save BiosBoy/63b872efc8a5e3f9034aa047410018a7 to your computer and use it in GitHub Desktop.
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