godzo101 godzo101 - 15 days ago 6
AngularJS Question

Get the object length in angularjs is undefined

How can I get the object length ?

In console my object looks like this:

Object {2: true, 3: true, 4: true}

.length will give me undefined. I just want to get the results = 3 ,for this case.

Thanks

Answer
var keys = Object.keys(objInstance);
var len = keys.length;

Where len is your "length."

There may be circumstances I haven't thought of where this doesn't work, but it should do what you need, given your example.

Comments