获取Json对象上的项目总数? [英] Get total number of items on Json object?

查看:586
本文介绍了获取Json对象上的项目总数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述


可能重复:

Javascript对象的长度(即关联数组)

我有一个类似于这个的对象:

I have an object similar to this one:

var jsonArray = {
  '-1': {
    '-1': 'b',
    '2': 'a',
    '10': 'c'
  },
  '2': {
    '-1': 'a',
    '2': 'b',
    '10': 'a'
  },
  '5': {
    '-1': 'a',
    '2': 'a',
    '10': 'b'
  }
};

我试图得到它的长度,问题是 jsonArray。长度返回5而不是3(这是它拥有的总项目)。该阵列相对较长(有1000x2000项),这必须每秒进行很多次。如何更有效地获得项目数?

I'm trying to get it's length, the problem is that jsonArray.length returns 5 instead of 3 (which is the total items it has). The array is relatively long (has 1000x2000 items) and this must be done a lot of times every second. How can I get the number of items more efficiently?

推荐答案

除了kieran的答案,显然现代浏览器的 Object.keys 功能。在这种情况下,您可以这样做:

In addition to kieran's answer, apparently, modern browsers have an Object.keys function. In this case, you could do this:

Object.keys(jsonArray).length;

此答案中有关如何列出javascript对象的属性

这篇关于获取Json对象上的项目总数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆