Skip to content

Instantly share code, notes, and snippets.

@yano3nora
Last active July 1, 2023 08:03
Show Gist options
  • Save yano3nora/c8de37d0ad8cc88c3b88b2c8377c7bc3 to your computer and use it in GitHub Desktop.
Save yano3nora/c8de37d0ad8cc88c3b88b2c8377c7bc3 to your computer and use it in GitHub Desktop.
[ts: Get object key by own value] #ts
import { objectKeys } from 'libs/utils/object-keys'
/**
* @example
* const obj = { one: 1 }
* const key = objectKeyByValue(obj, 1) // 'one' | undefined
*/
export const objectKeyByValue = <
T extends Record<PropertyKey, U>,
U = unknown
>(object: T, value: U) => {
return objectKeys(object).find(key => object[key] === value)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment