Last active
February 15, 2018 23:26
-
-
Save aaronranard/c82582b47dc6cd4a2a2ed8d956126c2c to your computer and use it in GitHub Desktop.
Key Value object to Key Value Pair Array
This file contains 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 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