Getting the size of an array in an object

meli picture meli · May 2, 2011 · Viewed 142k times · Source

I would like some help with getting the size of an array inside an object:

var st = { "itema":{...},"itemb":[{"id":"s01","cd":"c01","dd":"d01",....}{"id":"s02","cd":"c02","dd":"d02",....}]}

How would you get a count of the objects inside "itemb" (in this case 2)?

Answer

Andrew Hare picture Andrew Hare · May 2, 2011

Javascript arrays have a length property. Use it like this:

st.itemb.length