45

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?

Bhushan Gadekar
  • 13,485
  • 21
  • 82
  • 131

2 Answers2

104

You could try the following:

Object.keys(customer).length
Thierry Templier
  • 198,364
  • 44
  • 396
  • 360
14
Object.keys(this.customer).length
Ankit Singh
  • 24,525
  • 11
  • 66
  • 89