Knowing how to get the length of an object in Typescript will be very helpful when you are working with data you fetch from API. So how to do it? – Let’s go into detail
The solution to getting the length of an object in Typescript
You cannot 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 trick.
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 an object’s length in Typescript. You can get the length of the key by the Object.keys method and then get the length of it. In real life, you only should use for…loop in some particular situations.

Hello, guys! I hope that my knowledge in HTML, CSS, JavaScript, TypeScript, NodeJS, ReactJS, MongoDB, Python, MySQL, and npm computer languages may be of use to you. I’m Brent Johnson, a software developer.
Name of the university: HOU
Major: IT
Programming Languages: HTML, CSS, JavaScript, TypeScript, NodeJS, ReactJS, MongoDB, PyThon, MySQL, npm