object没有object.length,返回undefine
var data={"showdata":{"中国人民大学":{"201311":"24","201312":"98"}},"hotwords":["中国人民大学","北京大学","临沂大学","清华大学"]};
console.log(data.showdata.length);//undefine
console.log(data.hotwords.length);//4
1.访问对象长度通过for(var name in object)
function objlength(obj){
var n=0;
for(var name in obj){
n++;
}
return n;
}
objlength(data.showdata);//1
2.先将对象转化成数组,再用.length获取
var arr = Object.keys(data.showdata);
console.log(arr.length);//1