Number of elements in a javascript object

后端 未结 6 2184
借酒劲吻你
借酒劲吻你 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:12

    To do this in any ES5-compatible environment

    Object.keys(obj).length
    

    (Browser support from here)
    (Doc on Object.keys here, includes method you can add to non-ECMA5 browsers)

提交回复
热议问题