Skip to content

Instantly share code, notes, and snippets.

@stackdumper
Created March 22, 2018 15:34
Show Gist options
  • Save stackdumper/236ee1a03b47293b377c010bb9bdaf0c to your computer and use it in GitHub Desktop.
Save stackdumper/236ee1a03b47293b377c010bb9bdaf0c to your computer and use it in GitHub Desktop.
Map object to array recursively
/**
* Convert map object to multidimensional array
* @param {Object} object map object
* @return {Array} map array
*/
const mapObjectToArray = object =>
Object.keys(object).map((key) => {
if (typeof object[key] === 'object') {
return mapObjectToArray(object[key]);
} else if (typeof object[key] === 'number') {
return object[key];
}
throw new Error(`Unsupported element type in map: ${typeof object[key]}`);
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment