Last active
October 22, 2017 16:32
-
-
Save kf6kjg/14f1907cef9917ac265687c278f447ba to your computer and use it in GitHub Desktop.
JavaScript inline switch. Useful in React render methods, and still feels like a switch statement with the checked value up top. Also allows for falsy values which I also didn't see in my casual search for similar solutions.
This file contains 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
function iswitch(key, map, def) { | |
return map !== null && typeof map === 'object' && map.hasOwnProperty(key) ? map[key] : def; // This way falsy values can be returned. | |
} | |
let result = iswitch('a', { | |
'a': "first value", | |
'b': "second value", | |
}, "default value"); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment