Skip to content

Instantly share code, notes, and snippets.

@Lissy93
Last active November 11, 2019 18:09
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 Lissy93/0de24b3832caacb5e0e69b5ed6d7fc32 to your computer and use it in GitHub Desktop.
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
/**
* 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