如何获取JSON对象中数组的数量

3
以下是我的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",        
     }
   ]
 }"

数组是否只会出现在第一层,而不是嵌套对象中? - Phillip
感谢您的回答,在我的情况下,我需要使用json.stringify,即Object.keys(JSON.parse(obj)).length; - Sunil Chaudhary
可能是获取Json对象上的总项目数?的重复问题。 - cнŝdk
它始终在第一层级上,@Phillip.. - Sunil Chaudhary
4个回答

10

请尝试下面提到的代码:

Object.keys(jsonObject).length;

同时参考:如何获取Json对象上所有项目的总数?

要获取所有名称:

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

我们也可以获取名称吗? - Sunil Chaudhary
只需删除.length部分即可获得名称。现在你将拥有一个键的数组 :) - Phillip
@SunilChaudhary,var keys = Object.keys(jsonObject); // 获取标题 - Vivek Doshi
请注意,仅当对象仅包含数组时,此方法才有效。 - cнŝdk
如果没有数组,它会返回0吗? - Sunil Chaudhary
1
@SunilChaudhary,不,这是针对JSON对象的,所以不用担心。 - Binary Brackets

4
假设对象中的每个属性都包含一个数组,您可以使用 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);


1
你可以使用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);


0
在现代浏览器中,统计对象中的数组
Object.keys(obj).filter((e) => Array.isArray(obj[e])).length

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接