48

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?

0

2 Answers 2

114

You could try the following:

Object.keys(customer).length
Sign up to request clarification or add additional context in comments.

1 Comment

Is this O(1) or O(n)? I would expect .length to be O(1).
15
Object.keys(this.customer).length

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.