Last active
March 2, 2020 21:33
-
-
Save nhalstead/deaa80a956fa79cd531a7800f153b5f4 to your computer and use it in GitHub Desktop.
Convert Object to an Key Value List and vice versa
This file contains hidden or 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
/** | |
* Convert a Key Value List to a Key Value Pair | |
* | |
* Input: | |
* [{key: "enabled", value: true}] | |
* Output: | |
* {enabled: true} | |
* | |
* @param {object} list | |
* @return {object} | |
*/ | |
const keyListToObject = (list) => { | |
let obj = {}; | |
list.forEach((elm) => { | |
obj[ elm.key ] = elm.value; | |
}) | |
return obj; | |
} | |
/** | |
* Convert a Key Value Pair to a Key Value List | |
* | |
* Input: | |
* {enabled: true} | |
* Output: | |
* [{key: "enabled", value: true}] | |
* | |
* @param {object} obj | |
* @return {object} | |
*/ | |
const toKeyValueList = (obj) => { | |
let keys = Object.keys(obj); | |
return keys.map((key) => { | |
return {key: key, value: obj[key]} | |
}); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment