Length of a JavaScript object

前端 未结 30 3180
我在风中等你
我在风中等你 2020-11-21 04:35

I have a JavaScript object. Is there a built-in or accepted best practice way to get the length of this object?

const myObject = new Object();
myObject["         


        
30条回答
  •  梦毁少年i
    2020-11-21 05:15

    This method gets all your object's property names in an array, so you can get the length of that array which is equal to your object's keys' length.

    Object.getOwnPropertyNames({"hi":"Hi","msg":"Message"}).length; // => 2
    

提交回复
热议问题