Length of a JavaScript object

前端 未结 30 3069
我在风中等你
我在风中等你 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条回答
  •  傲寒
    傲寒 (楼主)
    2020-11-21 05:02

    If you need an associative data structure that exposes its size, better use a map instead of an object.

    const myMap = new Map();
    
    myMap.set("firstname", "Gareth");
    myMap.set("lastname", "Simpson");
    myMap.set("age", 21);
    
    console.log(myMap.size); // 3

提交回复
热议问题