Created
March 22, 2018 15:34
-
-
Save stackdumper/236ee1a03b47293b377c010bb9bdaf0c to your computer and use it in GitHub Desktop.
Map object to array recursively
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 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