javascript - How do I get number of arrays from an object -
i have json webservice in following format.
{ name:['a','b'], name:['cd','ef'], age:{...}, address:{...} }.
here have 2 arrays & 2 objects inside object , these (array & objects) numbers may vary. need is, how can number of arrays alone main object? there may exist way solve problem need code in .js (javascript file).
when tried:
object.keys(mainobject).length;
it gives total count of array + objects in main object.
var data = { name:['a','b'], othername:['cd','ef'], age:{a: 12}, address:{a: 'asdf'} } var numberofarrays = object.keys(data).filter(function(key) { return data[key] instanceof array; //or array.isarray(data[key]) if array created in frame }).length; alert(numberofarrays);
note: won't work in older versions of ie
to make work browsers don't support it, use shims mdn:
Comments
Post a Comment