【问题标题】:How to get number of arrays in a JSON object如何获取 JSON 对象中的数组数量
【发布时间】:2017-11-24 08:15:55
【问题描述】:

下面是我的 JSON。我想在这个对象中获取数组的数量和名称。这是动态创建的,所以我不知道它的编号和名称。本例中有 2 个数组,分别命名为 Table 和 Table1。

"{
      "Table": [
        {
          "Day": "Jan",
          "Counts": 20,
          "SrNo": 1,
          "Title": "test2",
          "ProfilePic": "/Image1.jpg" 
        },
        {
          "Day": "Feb",
          "Counts": 10,
          "SrNo": 2,        
          "Title": "test2",
          "ProfilePic": "/Image1.jpg"          
        }
    ],
 "Table1": [
    {
      "Day": "01",
      "Counts": 5,
      "SrNo": 1,       
      "Title": "test3",
      "ProfilePic": "/Image2.jpg"
     },
     {
      "Day": "02",
      "Counts": 9,
      "SrNo": 2,        
      "Title": "test3",
      "ProfilePic": "/Image2.jpg",        
     }
   ]
 }"

【问题讨论】:

  • 是否保证数组只会在第一个“级别”上,而不是在嵌套对象中?
  • 感谢您的回答,我的情况是我需要 json.stringify 即 Object.keys(JSON.parse(obj)).length;
  • 它总是在第一级@Phillip..

标签: javascript jquery arrays json


【解决方案1】:

试试下面提到的代码,

Object.keys(jsonObject).length;

另请参阅...:Get total number of items on Json object?

获取所有名称:

var keys = Object.keys(jsonObject); // this will return root level title ["Table" , "Table1"]

【讨论】:

  • 我们也可以得到名字吗?
  • 要获取名称,只需删除 .length 部分。您现在将拥有一个键数组:)
  • @SunilChaudhary , var keys = Object.keys(jsonObject); // 获取标题
  • 请注意,这仅在对象仅包含数组时才有效。
  • @SunilChaudhary ,不,这是针对 json 对象的,所以不用担心
【解决方案2】:

假设对象中的每个属性都包含一个数组,你可以使用Object.keys来统计属性的个数,像这样:

var arrayCount = Object.keys(obj).length;

或者,如果您真的想确定属性的类型,以防对象中有其他类型,您需要循环并单独检查每个属性,这可以使用filter() 来完成,如下所示:

var obj = {
  "Table": [{
      "Day": "Jan",
      "Counts": 20,
      "SrNo": 1,
      "Title": "test2",
      "ProfilePic": "/Image1.jpg"
    },
    {
      "Day": "Feb",
      "Counts": 10,
      "SrNo": 2,
      "Title": "test2",
      "ProfilePic": "/Image1.jpg"
    }
  ],
  "Table1": [{
      "Day": "01",
      "Counts": 5,
      "SrNo": 1,
      "Title": "test3",
      "ProfilePic": "/Image2.jpg"
    },
    {
      "Day": "02",
      "Counts": 9,
      "SrNo": 2,
      "Title": "test3",
      "ProfilePic": "/Image2.jpg",
    }
  ],
  'NotArray1': 'foo', // < not an array
  'isArray': false // < not an array
}

var arrayCount = Object.keys(obj).filter(function(key) {
  return obj[key].constructor === Array;
}).length;
console.log(arrayCount);

【讨论】:

    【解决方案3】:

    您可以使用Array.prototype.reduce() 来返回所有有效数组的对象属性值的总和:

    var obj = {
        "Table": [{
          "Day": "Jan",
          "Counts": 20,
          "SrNo": 1,
          "Title": "test2",
          "ProfilePic": "/Image1.jpg"
        }, {
          "Day": "Feb",
          "Counts": 10,
          "SrNo": 2,
          "Title": "test2",
          "ProfilePic": "/Image1.jpg"
        }],
        "Table1": [{
            "Day": "01",
            "Counts": 5,
            "SrNo": 1,
            "Title": "test3",
            "ProfilePic": "/Image2.jpg"
          }, {
            "Day": "02",
            "Counts": 9,
            "SrNo": 2,
            "Title": "test3",
            "ProfilePic": "/Image2.jpg"
          }
        ],
        "Table2": false
      },
      arrayCount = Object.keys(obj).reduce(function (acc, val) {
        return Array.isArray(obj[val]) ? ++acc : acc;
      }, 0);
    
    console.log(arrayCount);

    【讨论】:

      【解决方案4】:

      在现代浏览器中,计算对象中的数组

      Object.keys(obj).filter((e) => Array.isArray(obj[e])).length
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多