Tuesday, December 24, 2013

Size of dictionary in jquery

One would think that they can apply size of length function directly and get it, but its little more than that. You can not apply the length property or size function, both only works with list.

Dict is an Object and it doesn't have size method, and if you do length it will give 'undefined'.

Solution - Get the array of keys and perform length.

e.g.
> dictObj = Object {PointAArray[23]PointBArray[23]}
Object.keys(dictObj).length
> 2

Note - It won't works > IE8

Other robust way is as described in this SO answer, add size function on Object and use it across the app and it will work cross browser.

Short answer is there is no built-in way.