53

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)?

Cerbrus
  • 70,800
  • 18
  • 132
  • 147
meli
  • 533
  • 1
  • 4
  • 5

2 Answers2

87

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

st.itemb.length
Jonathan Leffler
  • 730,956
  • 141
  • 904
  • 1,278
Andrew Hare
  • 344,730
  • 71
  • 640
  • 635
19

Arrays have a property .length that 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
Jamie Treworgy
  • 23,934
  • 8
  • 76
  • 119
BrunoLM
  • 97,872
  • 84
  • 296
  • 452