Last active
November 11, 2019 18:09
-
-
Save Lissy93/0de24b3832caacb5e0e69b5ed6d7fc32 to your computer and use it in GitHub Desktop.
A simple function to convert any flat JSON object to an array of keys and values
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
/** | |
* Copyright Alicia Sykes <https://aliciasykes.com> | |
* Licensed under MIT X11: https://git.io/Jew4i | |
* | |
* Converts a given JSON object into an array | |
* Where { key: 'Value' } --> ['key': 'value'] | |
* @param json - the object to be converted | |
*/ | |
const json2array = (json) => { | |
const result = []; | |
const keys = Object.keys(json); | |
keys.forEach((key) => { | |
result.push({ key, value: json[key] }); | |
}); | |
return result; | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment