How To Get The Length Of An Object In Typescript

Get the length of an Object in TypeScript

To get the length of an object in Typescript you can use Object.keys method. It will be very helpful when you are working with data you fetch from API. So how to do it? Let’s go into detail now.

Get the length of an object in TypeScript

We can’t directly iterate over the object like with an array, so we cannot get the length of the object by length method like with an array. We will need to do it manually or with some tricks.

Using Object.keys

The Object.keys method will help you return an array of keys inside the object. 

The syntax:

Object.keys(obj)

Parameters: 

  • obj: The object that you want to get an array of key.

Example:

const personDetail: object = {
    name: 'Tremayne Bernier',
    age: 21,
    date: '2001-03-15',
    userName: 'hpacocha',
    company: 'Kling Group',
    height: 182,
    weight: 86.3
}
 
// Get an array of keys
const keyArray: any[] = Object.keys(personDetail)
 
console.log(keyArray)
console.log(keyArray.length)

Output:

[LOG]: ["name", "age", "date", "userName", "company", "height", "weight"]
[LOG]: 7 

If you want to get the length of the object and value, you can use the Object.values method. That method will return an array of values in the object.

Syntax:

Object.values(obj)

Parameters:

  • obj: The object that you want to get an array of values

Example:

const personDetail: object = {
    name: 'Tremayne Bernier',
    age: 21,
    date: '2001-03-15',
    userName: 'hpacocha',
    company: 'Kling Group',
    height: 182,
    weight: 86.3
}
 
// Get an array of values
const valuesArray: any[] = Object.values(personDetail)
 
console.log(valuesArray)
console.log(valuesArray.length)

Output:

[LOG]: ["Tremayne Bernier", 21, "2001-03-15", "hpacocha", "Kling Group", 182, 86.3]
[LOG]: 7 

Using for…in loop

You can manually get the Object’s length by using for…in loop. The for…in loop will iterate over the object then you can execute code for each loop.

Syntax:

for (key in Object)

Parameters:

  • key: Each key of the object through each loop.
  • object: The object that you want to loop over.

Example:

const personDetail: object = {
    name: 'Tremayne Bernier',
    age: 21,
    date: '2001-03-15',
    userName: 'hpacocha',
    company: 'Kling Group',
    height: 182,
    weight: 86.3
}
 
// Create a flag to store the length value of the Object
let objLength = 0
 
// For in loop will loop over all keys of the Object
for (let key in personDetail) {
    console.log(key)
    objLength++   
}
 
console.log(objLength)

Output:

[LOG]: "name"
[LOG]: "age" 
[LOG]: "date" 
[LOG]: "userName" 
[LOG]: "company" 
[LOG]: "height" 
[LOG]: "weight" 
[LOG]: 7

As you see here I create an objLength variable like a flag to store value. Each time loop will be plus one. And the final result will be the length of the object.

Summary

In this article, I showed you how to get the length of an object in TypeScript in 2 ways. Reality, you only should use for…loop in some particular situations.

Maybe you are interested:

Leave a Reply

Your email address will not be published. Required fields are marked *