0

I have an object which contains multiple arrays. I need to know the number of arrays in this object.

This is the code which creates the map object:

function parseXML(xData, Status){
  var map = {}; //init the map
  var web = $(xData.responseXML).find("Web");
  for (var i = 0; i < web.length; i++) {
  //we create a index for our links based on the depth of them by `/`
    var m = web[i].attributes['Url'].value.substring(23, web[i].attributes['Url'].value.length).split('/').length; 

    map[m] = map[m] || []; //make sure we leave alone the old values if there is none init with new array
    map[m].push(web[i].attributes['Url'].value); //push new value to node
  }
  console.log(map);
  createNav(map);
}

I've tried:

console.log('there are '+map.length+"levels in this site"); but I get map.length = undefined.

Batman
  • 4,075
  • 12
  • 59
  • 122
  • You need to [iterate over the properties](http://stackoverflow.com/questions/126100/how-to-efficiently-count-the-number-of-keys-properties-of-an-object-in-javascrip) and [check whether the value of that property is an array](http://stackoverflow.com/questions/767486/how-do-you-check-if-a-variable-is-an-array-in-javascript). While you're iterating, count up. – nemo Oct 20 '13 at 00:56
  • Why don't you get it from web.length? You can read it from there, and add it as an extra property of your object. – bfavaretto Oct 20 '13 at 00:56

1 Answers1

3
map.length = 0;
for (item in map) {
    if (map.hasOwnProperty(item) && map[item] instanceof Array) {
        map.length++
    }
}
Nicolás Straub
  • 3,322
  • 5
  • 19
  • 42