javascript JSON - 查找对象的长度

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3385820/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-25 01:02:40  来源:igfitidea点击:

JSON - Find the length of objects

javascriptjson

提问by Vinothkumar Arputharaj

HI,

你好,

I'm having a JSON parsed return object set.

我有一个 JSON 解析的返回对象集。

{
  "word":[
      "offered",
      "postings"
  ],

  "annotation":[
      ["offered highlighted","this is also given as annotation","This annotation has been added here currently","offering new annotation points","\"offered\" is in the sense of languages"],
      ["my postings","this is new annotation for postings.","my postings","this is posting annotation.... Working for the feature of the annotation."]
  ],

  "user":[
  ["","","vinoth","Anonymous","Vinoth"],
  ["Anonymous","vinoth","Anonymous","Arputharaj"]
  ],

  "id":[
  ["58","60","61","63","68"],
  ["32","57","59","62"]
  ],

  "comment":
  {
    "id58":["first comment","This is a old comment for this annotation","Next level of commenting.","Fourth comment to this annotation","testing"],
    "id61":["this is an old annotation.\r\nMy comment is very bad about this.","Second comment to this annotation"],
    "id57":["I want to add one more comment to this"]
    },

    "commentUser":{
      "id58":["vinoth","Anonymous","Vinothkumar","Vinothkumar","vinoth"],
      "id61":["Anonymous","Commentor"],
      "id57":["vinoth"]
      }
  }

I want to know about the length of each object and array.

我想知道每个对象和数组的长度。

I've used .lengthto get the length of annotation[0].length. I'm getting the expected result i.e.: 5. The same is to "user" & "id".

我曾经.length得到annotation[0].length. 我得到了预期的结果,即:5。“用户”和“id”也是如此。

But I'm not getting the lengths of "word", "id58", "id61", etc... Also I want to know about the length of the comment& commentUser.

但我没有得到“word”、“id58”、“id61”等的长度……我也想知道comment&的长度commentUser

Please help me in this.

请帮助我。

回答by Daniel Earwicker

To count the number of properties an object has, you need to loop through the properties but remember to use the hasOwnPropertyfunction:

要计算对象具有的属性数量,您需要遍历这些属性,但请记住使用该hasOwnProperty函数:

var count = 0;
for (var p in obj) {
    if (obj.hasOwnProperty(p)) {
        count++;
    }
}

If you forget to do that, you will be looping through inherited properties. If you (or some library) has assigned a function to the prototype of Object, then all objects will seem to have that property, and thus will seem one item "longer" than they intrinsically are.

如果您忘记这样做,您将遍历继承的属性。如果您(或某个库)已将一个函数分配给 的原型Object,那么所有对象似乎都具有该属性,因此似乎一个项目比它们本质上“长”。

To avoid the need to remember this, consider using jQuery's eachinstead:

为避免记住这一点,请考虑使用 jQuery each

var count = 0;
$.each(obj, function(k, v) { count++; });

UPDATEWith current browsers:

使用当前浏览器更新

Object.keys(someObj).length

回答by AKX

The value for the key wordin your example (let's call it obj) is an array, so obj.word.lengthshould be 2.

word您示例中的键值(我们称之为obj)是一个数组,所以obj.word.length应该是 2。

The value for commentand commentUseris an object, which does not have length per se, so you'll need to count them yourself:

commentand的值commentUser是一个对象,它本身没有长度,所以你需要自己计算它们:

var length=0;
for(var dummy in obj.comment) length++;