Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Object length in typescript? [duplicate]

is there any way in typescript by which I can get length of an object:

Something like this:

say I have an object:

public customer:any={      "name":"Bhushan",      "eid":"879546",      "dept":"IT" } 

Now I am trying to get its length in typescript.

ie. when I am doing customer.length(), I should be able to get value 3 as it has 3 elements.

I tried Object.getOwnPropertyNames(customer.value) but its returning 2 whereas I have 3 elements in my object.

any inputs?

like image 601
Bhushan Gadekar Avatar asked Jun 28 '16 11:06

Bhushan Gadekar


People also ask

How do you find the length of a object in TypeScript?

To get the length of an object in TypeScript:Use the Object. keys() method to get an array of the object's keys. Access the length property on the array of keys. The length property will return the number of key-value pairs in the object.

How do you find the length of an object?

Answer: Use the Object. keys() Method You can simply use the Object. keys() method along with the length property to get the length of a JavaScript object. The Object. keys() method returns an array of a given object's own enumerable property names, and the length property returns the number of elements in that array.


2 Answers

You could try the following:

Object.keys(customer).length 
like image 65
Thierry Templier Avatar answered Sep 21 '22 23:09

Thierry Templier


Object.keys(this.customer).length 
like image 42
Ankit Singh Avatar answered Sep 22 '22 23:09

Ankit Singh