代码:
var animals = {
"elephant": {
"name" : "Bingo",
"age" : "4"
},
"Lion": {
"name" : "Tango",
"age" : "8"
},
"Tiger": {
"name" : "Zango",
"age" : "7"
}
}
我想计算这个对象字面量中使用 Jquery 的对象的数量。
最佳答案
你可以使用 Object.keys(animals).length
或者
var count = 0;
for (var animal in animals) {
if (animals.hasOwnProperty(animal)) {
count++;
}
}
// `count` now holds the number of object literals in the `animals` variable
或者可能是也可能不是最有效的许多 jQuery 解决方案之一:
var count = $.map(animals, function(n, i) { return i; }).length;
关于javascript - 使用 Jquery 计算对象字面量中的对象数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13802987/