Skip to content

Instantly share code, notes, and snippets.

@jordyvandomselaar
Created August 19, 2020 12:41
Show Gist options
  • Save jordyvandomselaar/f78c23501654511aa816a3a9ecde721c to your computer and use it in GitHub Desktop.
Save jordyvandomselaar/f78c23501654511aa816a3a9ecde721c to your computer and use it in GitHub Desktop.
Get key by value
// 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