javascript - 使用javascript计算数组数组的长度

标签 javascript

我有这个数组,我想计算其中的总数,谁能告诉我如何使用纯 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);

Object.keys()

The Object.keys() method returns an array of a given object's own enumerable properties, in the same order as that provided by a for...in loop (the difference being that a for-in loop enumerates properties in the prototype chain as well).

Array#length

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/

相关文章:

javascript - 如何初始化多个路由器?

javascript - 如何在 CSS 和 Jquery 上设置 float 位置弹出消息

javascript - InDesign 启动脚本不从 XML 导入加载图像的问题

javascript - 使用 JQuery 计时器调用 js-function

javascript - AngularJS 可以做到这一点吗?

java - js判断iframe是否包含图片

Javascript从数组中的对象获取值

javascript - .click() 函数不起作用 javascript/jquery

javascript - 在 Javascript 中使用 new Date() 测量时间的问题

javascript - HTML5 Range 功能在移动设备上损坏