[removed] array.length returns undefined

后端 未结 5 1153
执念已碎
执念已碎 2020-12-23 19:29

I have a set of data that is being passed on by the PHP\'s json_encode function. I\'m using the jQuery getJSON function to decode it:



        
5条回答
  •  一整个雨季
    2020-12-23 19:54

    One option is:

    Object.keys(myObject).length
    

    Sadly it not works under older IE versions (under 9).

    If you need that compatibility, use the painful version:

    var key, count = 0;
    for(key in myObject) {
      if(myObject.hasOwnProperty(key)) {
        count++;
      }
    }
    

提交回复
热议问题