Last active
March 11, 2020 10:07
-
-
Save decentraliser/800815ebbda78b65836a830693e15fce to your computer and use it in GitHub Desktop.
Extract strings and numbers from enums
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
// 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