Skip to content

Instantly share code, notes, and snippets.

@aercolino
Created January 23, 2020 13:26
Show Gist options
  • Save aercolino/98cf08b83bf098213a09725425bff48e to your computer and use it in GitHub Desktop.
Save aercolino/98cf08b83bf098213a09725425bff48e to your computer and use it in GitHub Desktop.

Revisions

  1. aercolino created this gist Jan 23, 2020.
    13 changes: 13 additions & 0 deletions convertDottedKeyToObject.js
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,13 @@
    function convertDottedKeyToObject(name, value) {
    return name.split('.').reverse().reduce((acc, key) => ({ [key]: acc }), value);
    }

    JSON.stringify(convertDottedKeyToObject('it.is.a', 'one liner'), null, 2)

    "{
    "it": {
    "is": {
    "a": "one liner"
    }
    }
    }"