To count the number of objects in object literal using Jquery

前端 未结 3 1702
旧巷少年郎
旧巷少年郎 2021-02-06 04:45

Code:

var animals = {
                    \"elephant\": {
                                    \"name\" : \"Bingo\",
                                    \"age\" :         


        
3条回答
  •  無奈伤痛
    2021-02-06 05:42

    Can't you use an array?

    Anyway, in an object, you can do that:

    Object.prototype.length = function() {
        var count = 0, k=null;
        for (k in this) {
            if (this.hasOwnProperty(k)) count++;
        }
        return count;
    }
    console.log( animals.length() );
    

提交回复
热议问题