如何有效地計算JavaScript中對象的鍵/屬性數量?

問題:

What's the fastest way to count the number of keys/properties of an object? 計算對象的鍵/屬性數的最快方法是什麼? It it possible to do this without iterating over the object? 是否能夠在不迭代對象的狀況下執行此操做? ie without doing 即不作 ide

var count = 0;
for (k in myobj) if (myobj.hasOwnProperty(k)) count++;

(Firefox did provide a magic __count__ property, but this was removed somewhere around version 4.) (Firefox確實提供了一個神奇的__count__屬性,但此屬性已在版本4的某個位置刪除。) this


解決方案:

參考一: https://stackoom.com/question/Wns/如何有效地計算JavaScript中對象的鍵-屬性數量
參考二: https://oldbug.net/q/Wns/How-to-efficiently-count-the-number-of-keys-properties-of-an-object-in-JavaScript
相關文章
相關標籤/搜索