Number of elements in a javascript object

后端 未结 6 2153
借酒劲吻你
借酒劲吻你 2020-12-12 17:46

Is there a way to get (from somewhere) the number of elements in a javascript object?? (i.e. constant-time complexity).

I cant find a property or method that retriev

6条回答
  •  一生所求
    2020-12-12 18:18

    function count(){
        var c= 0;
        for(var p in this) if(this.hasOwnProperty(p))++c;
        return c;
    }
    
    var O={a: 1, b: 2, c: 3};
    
    count.call(O);
    

提交回复
热议问题