Last updated: Feb 27, 2024
Reading timeยท3 min
To get the length of an object in TypeScript:
Object.keys()
method to get an array of the object's keys.length
property on the array of keys.interface Person { id: number; name: string; age: number; } const obj: Person = { id: 1, name: 'bobby hadz', age: 30 }; // ๐๏ธ const length: number const len = Object.keys(obj).length; console.log(len); // ๐๏ธ 3
We used the Object.keys method to get an array of the object's keys.
// ๐๏ธ ['id', 'name'] console.log(Object.keys({ id: 1, name: 'Bobby Hadz' }));
The only parameter the method takes is the object for which the keys are returned.
length
property on the array to get the number of key-value pairs in the object.interface Person { id: number; name: string; age: number; } const obj: Person = { id: 1, name: 'bobby hadz', age: 30 }; // ๐๏ธ const length: number const len = Object.keys(obj).length; console.log(len); // ๐๏ธ 3
Alternatively, you can use a for...in
loop.
An alternative approach is to initialize a length
variable, set it to 0
and
use a
for...in
loop to iterate over the object.
for...in
loop in TypeScriptThis is a three-step process:
length
variable and initialize it to 0
.for...in
loop to iterate over the object.length
variable on each iteration.interface Person { id: number; name: string; age: number; } const obj: Person = { id: 1, name: 'Bobby Hadz', age: 30 }; let len = 0; for (const key in obj) { if (obj.hasOwnProperty(key)) { len += 1; } } console.log(len); // ๐๏ธ 3
The for...in
loops iterates over the object's enumerable properties, including
the inherited ones.
This is why we had to use the Object.hasOwnProperty method to check if the property exists directly on the object or is inherited.
We only increment the length
variable if the property exists directly on the
object.
After the last iteration, the length
variable stores a number representing the
length of the object.
Object.values()
You can also use the Object.values()
method to get the length of an object.
interface Person { id: number; name: string; age: number; } const obj: Person = { id: 1, name: 'Bobby Hadz', age: 30 }; const len = Object.values(obj).length; console.log(len); // ๐๏ธ 3
The Object.values() method returns an array of the object's values.
interface Person { id: number; name: string; age: number; } const obj: Person = { id: 1, name: 'Bobby Hadz', age: 30 }; // ๐๏ธ [ 1, 'Bobby Hadz', 30 ] console.log(Object.values(obj));
Object.entries()
The Object.entries()
method can also be used to get an object's length.
interface Person { id: number; name: string; age: number; } const obj: Person = { id: 1, name: 'Bobby Hadz', age: 30 }; const len = Object.entries(obj).length; console.log(len); // ๐๏ธ 3
The Object.entries method returns an array of the object's key-value pairs
interface Person { id: number; name: string; age: number; } const obj: Person = { id: 1, name: 'Bobby Hadz', age: 30 }; // ๐๏ธ [ [ 'id', 1 ], [ 'name', 'Bobby Hadz' ], [ 'age', 30 ] ] console.log(Object.entries(obj));
The first element in each nested array is the key and the second is the value.
Which approach you pick is a matter of personal preference. I'd use the
Object.keys()
method as it is most commonly used to get an object's length and
is quite intuitive.