Skip to content

Instantly share code, notes, and snippets.

@aaronranard
Last active February 15, 2018 23:26
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save aaronranard/c82582b47dc6cd4a2a2ed8d956126c2c to your computer and use it in GitHub Desktop.
Save aaronranard/c82582b47dc6cd4a2a2ed8d956126c2c to your computer and use it in GitHub Desktop.
Key Value object to Key Value Pair Array
const keyValueObj = { "api_url": "https://hawaii.usa.gov", "message": "BALLISTIC MISSILE INBOUND.", "is_drill": true };
const keyValueArray = Object.entries(keyValueObj).map(([key, value]) => ({ key, value }));
/**
* keyValueArray:
* [
* { key: "api_url", value: "https://hawaii.usa.gov" },
* { key: "message", value: "BALLISTIC MISSILE INBOUND." },
* { key: "is_drill", value: true },
* ],
*
*/
const backToKeyValueObj = keyValueArray.reduce((obj, { key, value }) => ({ ...obj, [key]: value}), {})
/**
* backToKeyValueObj:
* { "api_url": "https://hawaii.usa.gov", "message": "BALLISTIC MISSILE INBOUND.", "is_drill": true }
*/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment