最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - JSON - Find the length of objects - Stack Overflow

programmeradmin2浏览0评论

HI,

I'm having a JSON parsed return object set.

{
  "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"]
  ],

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

    "mentUser":{
      "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 .length to get the length of annotation[0].length. I'm getting the expected result i.e.: 5. The same is to "user" & "id".

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

Please help me in this.

HI,

I'm having a JSON parsed return object set.

{
  "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"]
  ],

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

    "mentUser":{
      "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 .length to get the length of annotation[0].length. I'm getting the expected result i.e.: 5. The same is to "user" & "id".

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

Please help me in this.

Share Improve this question edited Aug 2, 2010 at 7:31 skaffman 404k96 gold badges824 silver badges775 bronze badges asked Aug 2, 2010 at 7:31 Vinothkumar ArputharajVinothkumar Arputharaj 4,5694 gold badges30 silver badges36 bronze badges
Add a ment  | 

2 Answers 2

Reset to default 7

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

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.

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

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

UPDATE With current browsers:

Object.keys(someObj).length

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

The value for ment and mentUser is an object, which does not have length per se, so you'll need to count them yourself:

var length=0;
for(var dummy in obj.ment) length++;
发布评论

评论列表(0)

  1. 暂无评论