Skip to content

Instantly share code, notes, and snippets.

@decentraliser
Last active March 11, 2020 10:07
Show Gist options
  • Save decentraliser/800815ebbda78b65836a830693e15fce to your computer and use it in GitHub Desktop.
Save decentraliser/800815ebbda78b65836a830693e15fce to your computer and use it in GitHub Desktop.
Extract strings and numbers from enums
// A simple way to extract strings and numbers from an enum
enum actions {
Link = 1,
Unlink = 0
}
const strings = Object.keys(actions)
.filter((key) => Number.isNaN(parseFloat(key)))
const numbers = Object.keys(actions)
.filter((key) => !Number.isNaN(parseFloat(key)))
.map(x => parseInt(x, 10))
console.log('strings', strings) // [ 'Link', 'Unlink' ]
console.log('numbers', numbers) // [ 0, 1 ]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment