Last active
May 23, 2018 02:32
-
-
Save srph/7ed8b3ffe9fd2b3ca21ac0037f15c3d7 to your computer and use it in GitHub Desktop.
JS: Array.from that works without .length
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
/** | |
* Converts an array-like object into an array. | |
* Array.from doesn't work if an object doesn't have a "length" property. | |
* e.g., Array.from({ 0: 1 }) doesn't work; Array.from({ 0: 1, length: 1 }) does. | |
*/ | |
function arrayFrom (obj: {}): Array<*> { | |
return Object.keys(obj).reduce((set: Array, key: string) => { | |
return [...set, obj[key]] | |
}, []) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment