Created
August 19, 2020 12:41
-
-
Save jordyvandomselaar/f78c23501654511aa816a3a9ecde721c to your computer and use it in GitHub Desktop.
Get key by value
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
// Easily get object key by value | |
const referrerSources = { | |
"aws": "AWS Cloud", | |
"oudesoft": "Oude software", | |
"mvp": "Voordelen van een MVP", | |
"corona": "Corona Consult" | |
} | |
// Object entries transforms an object into an array with sub arrays with key & value: | |
// [ | |
// [ 'aws', 'AWS Cloud' ], | |
// [ 'oudesoft', 'Oude software' ], | |
// [ 'mvp', 'Voordelen van een MVP' ], | |
// [ 'corona', 'Corona Consult' ] | |
// ] | |
// Then loop through the array and return the value. | |
const a = Object.entries(referrerSources).find(referrerSource => referrerSource[1] === "AWS Cloud")?.[0]; | |
console.log(a) // "aws" | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment