Skip to content

Instantly share code, notes, and snippets.

@srph
Last active May 23, 2018 02:32
Show Gist options
  • Save srph/7ed8b3ffe9fd2b3ca21ac0037f15c3d7 to your computer and use it in GitHub Desktop.
Save srph/7ed8b3ffe9fd2b3ca21ac0037f15c3d7 to your computer and use it in GitHub Desktop.
JS: Array.from that works without .length
/**
* 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