我有这个数组,我想计算其中的总数,谁能告诉我如何使用纯 javascript 实现这个
var total = {
"numbers":{
"1":"9897877884",
"2":"9867543234",
"3":"7898879900",
"4":"1234567890"
}
}
最佳答案
您可以使用 Object.keys(total.numbers).length
。第一部分返回一个包含键的数组,第二部分获取它的长度。
var total = { "numbers": { "1": "9897877884", "2": "9867543234", "3": "7898879900", "4": "1234567890" } };
console.log(Object.keys(total.numbers).length);
The
Object.keys()
method returns an array of a given object's own enumerable properties, in the same order as that provided by afor...in
loop (the difference being that a for-in loop enumerates properties in the prototype chain as well).
The
length
property represents an unsigned, 32-bit integer that is always numerically greater than the highest index in the array.
关于javascript - 使用javascript计算数组数组的长度,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37834307/