Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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
You can’t perform that action at this time.