获取Object中的数组数量

时间:2013-10-20 00:52:34

标签: javascript jquery arrays 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);
}

我试过了:

console.log(此网站中有'+ map.length +“级别”);但我得到map.length = undefined。

1 个答案:

答案 0 :(得分:3)

map.length = 0;
for (item in map) {
    if (map.hasOwnProperty(item) && map[item] instanceof Array) {
        map.length++
    }
}
相关问题