Skip to content

Instantly share code, notes, and snippets.

@jerkovicl
Last active February 8, 2019 19:59
Show Gist options
  • Save jerkovicl/9dc8ad82c1ab10b43971dfa44e29a723 to your computer and use it in GitHub Desktop.
Save jerkovicl/9dc8ad82c1ab10b43971dfa44e29a723 to your computer and use it in GitHub Desktop.
transpose a javascript object into a key/value array
var obj =  {
        species: 'Lemon Candy',
        occupation: 'Earl, Heir to the Candy Kingdom Throne',
        type: 'select',
        key: 'name',
        options:[]
      }

var output = Object.entries(obj).map(([filterKey, filterValue]) => ({filterKey,filterValue}));
console.log(output);
[
  {
    "filterKey": "species",
    "filterValue": "Lemon Candy"
  },
  {
    "filterKey": "occupation",
    "filterValue": "Earl, Heir to the Candy Kingdom Throne"
  },
  {
    "filterKey": "type",
    "filterValue": "select"
  },
  {
    "filterKey": "key",
    "filterValue": "name"
  },
  {
    "filterKey": "options",
    "filterValue": []
  }
]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment