Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
395 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 Answer

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...