Get the length of an Object in TypeScript

avatar
Borislav Hadzhiev

Last updated: Feb 27, 2024
3 min

banner

# Get the length of an Object in TypeScript

To get the length of an object in TypeScript:

  1. Use the Object.keys() method to get an array of the object's keys.
  2. Access the length property on the array of keys.
index.ts
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

get length of object in typescript

The code for this article is available on GitHub

We used the Object.keys method to get an array of the object's keys.

index.ts
// ๐Ÿ‘‡๏ธ ['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.

The last step is to access the length property on the array to get the number of key-value pairs in the object.
index.ts
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.

# Get the Length of an Object using a for...in loop in TypeScript

This is a three-step process:

  1. Declare a length variable and initialize it to 0.
  2. Use a for...in loop to iterate over the object.
  3. Increment the value of the length variable on each iteration.
index.ts
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 code for this article is available on GitHub

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.

# Get the Length of an Object using Object.values()

You can also use the Object.values() method to get the length of an object.

index.ts
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

get length of object using object values

The code for this article is available on GitHub

The Object.values() method returns an array of the object's values.

index.ts
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));

# Get the Length of an Object using Object.entries()

The Object.entries() method can also be used to get an object's length.

index.ts
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

get length of object using object entries

The code for this article is available on GitHub

The Object.entries method returns an array of the object's key-value pairs

index.ts
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.

I wrote a book in which I share everything I know about how to become a better, more efficient programmer.
book cover
You can use the search field on my Home Page to filter through all of my articles.

Copyright ยฉ 2024 Borislav Hadzhiev