Last updated: Feb 27, 2024
Reading timeยท3 min
To get an object's key by value in TypeScript:
Object.keys()
method to get an array of the object's keys.find()
method to get the key by its value.const obj = { name: 'Bobby Hadz', department: 'accounting', country: 'Chile', }; // โ Using Object.keys() const result1 = (Object.keys(obj) as (keyof typeof obj)[]).find((key) => { return obj[key] === 'accounting'; }); console.log(result1); // ๐๏ธ "department"
If you need to get an object's value by key, use bracket or dot notation instead.
// โ bracket notation console.log(obj['department']); // accounting // โ dot notation console.log(obj.name); // Bobby Hadz console.log(obj.country); // Chile
If the name of the key is stored in a variable, use bracket notation.
const obj = { name: 'Bobby Hadz', department: 'accounting', country: 'Chile', }; const key = 'department'; // โ with key stored in a variable console.log(obj[key]); // accounting
We used the Object.keys() method to get an object's key by its value.
The method returns an array of the object's keys.
const obj = { name: 'Bobby Hadz', department: 'accounting', country: 'Chile', }; // ๐๏ธ const r: string[] const r = Object.keys(obj); console.log(r); // ๐๏ธ ['name', 'department', 'country']
Object.keys()
method as string[]
.All of the object's keys are strings, but not all strings are keys in the object, so we wouldn't be able to access a value by key directly.
const obj = { name: 'Bobby Hadz', department: 'accounting', country: 'Chile', }; // ๐๏ธ const r: string[] const r = Object.keys(obj).find((key) => { // โ๏ธ Error: No index signature with a parameter of // type 'string' was found on type // '{ name: string; department: string; country: string; }'. return obj[key] === 'accounting'; });
TypeScript is telling us that we can't index the object with any string key, it
has to be name
, department
or country
.
I've also written an article on how to create a type from an object's keys or values.
This is why we used a
type assertion
to type the return value of the Object.keys()
method.
const obj = { name: 'Bobby Hadz', department: 'accounting', country: 'Chile', }; // now the `key` parameter is typed as // ๐๏ธ (parameter) key: "name" | "department" | "country" const result1 = (Object.keys(obj) as (keyof typeof obj)[]).find((key) => { return obj[key] === 'accounting'; }); console.log(result1); // ๐๏ธ "department"
Now the key
parameter in the find
method is a
union type of the objects keys, so
everything works as expected.
The function we passed to the Array.find method gets called with each element in the array of keys until it returns a truthy value or iterates over the entire array.
find()
method returns the corresponding array element and short-circuits.If the condition is never satisfied, find()
returns undefined
.
Object.entries()
method to get an object's key by valueYou can also use the Object.entries()
method to get an object's key by its
value.
const obj = { name: 'Bobby Hadz', department: 'accounting', country: 'Chile', }; let result2 = ''; Object.entries(obj).find(([key, value]) => { if (value === 'accounting') { result2 = key; return true; } return false; }); console.log(result2); // ๐๏ธ "department"
Notice that we don't have to use type assertions when using this approach.
The Object.entries() method returns an array of key-value pairs.
const obj = { name: 'Bobby Hadz', department: 'accounting', country: 'Chile', }; // ๐๏ธ const r: [string, string][] const r = Object.entries(obj); // ๐๏ธ [['name', 'Bobby Hadz'], ['department', 'accounting'], ['country', 'Chile']] console.log(r);
2
elements - the key and value.The find()
method returns the array element for which the condition is met or
an undefined
value and we only want the key, so it's simpler to:
You can learn more about the related topics by checking out the following tutorials: