[removed] array.length returns undefined

后端 未结 5 1159
执念已碎
执念已碎 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 20:06

    Objects don't have a .length property.

    A simple solution if you know you don't have to worry about hasOwnProperty checks, would be to do this:

    Object.keys(data).length;
    

    If you have to support IE 8 or lower, you'll have to use a loop, instead:

    var length= 0;
    for(var key in data) {
        if(data.hasOwnProperty(key)){
            length++;
        }
    }
    

提交回复
热议问题