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?
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.
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.
You could try the following:
Object.keys(customer).length
Object.keys(this.customer).length
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With