Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
531 views
in Technique[技术] by (71.8m points)

javascript - 如何有效地计算JavaScript中对象的键/属性数量?(How to efficiently count the number of keys/properties of an object in 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(即不做) 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的某个位置删除。))   ask by mjs translate from so

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

To do this in any ES5-compatible environment , such as Node , Chrome, IE 9+, Firefox 4+, or Safari 5+:(要在任何与ES5兼容的环境 (例如Node ,Chrome,IE 9+,Firefox 4+或Safari 5+)中执行此操作:)

Object.keys(obj).length Browser compatibility(浏览器兼容性) Object.keys documentation (includes a method you can add to non-ES5 browsers)(Object.keys文档 (包括可以添加到非ES5浏览器的方法))

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...