如何有效地计算JavaScript中对象的键/属性数量?

旧巷老猫 提交于 2020-08-09 05:45:34

问题:

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 即不做

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的某个位置删除。)


解决方案:

参考一: 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
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!