Getting the size of an array in an object

I need help 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 the count of objects inside "itemb" (in this case 2)?

+43
javascript object arrays count
May 02 '11 at 19:30
source share
2 answers

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

 st.itemb.length 
+77
May 2 '11 at 19:31
source share

Arrays have the .length property, which returns the number of elements.

 var st = { "itema":{}, "itemb": [ {"id":"s01","cd":"c01","dd":"d01"}, {"id":"s02","cd":"c02","dd":"d02"} ] }; st.itemb.length // 2 
+15
May 2 '11 at 19:33
source share



All Articles