javascript 对象中的元素数量
有没有办法(从某处)获取 Javascript 对象中的元素数量? (即恒定时间复杂度)。
我找不到检索该信息的属性或方法。 到目前为止,我只能想到对整个集合进行迭代,但这是线性时间。
奇怪的是,无法直接访问对象的大小,您不觉得吗?
编辑:
我说的是 Object
对象(不是一般的对象):
var obj = new Object ;
Is there a way to get (from somewhere) the number of elements in a Javascript object?? (i.e. constant-time complexity).
I can't find a property or method that retrieves that information. So far I can only think of doing an iteration through the whole collection, but that's linear time.
It's strange there is no direct access to the size of the object, don't you think.
EDIT:
I'm talking about the Object
object (not objects in general):
var obj = new Object ;
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
尽管 JS 实现可能会在内部跟踪这样的值,但没有标准的方法来获取它。
过去,Mozilla 的 Javascript 变体暴露了 非标准 < code>__count__,但已在 1.8.5 版本中删除。
对于跨浏览器脚本编写,您必须显式迭代属性并检查
hasOwnProperty()
:在支持 ECMAScript 5 的实现的情况下,这也可以写为(感谢 Avi Flax)
请记住,您还会错过不可枚举的属性(例如数组的
长度
) 。如果您使用的是 jQuery、Prototype、Mootools、$whatever-the-newest-hype 等框架,请检查它们是否带有自己的集合 API,这可能比使用本机 JS 对象更好地解决您的问题。
Although JS implementations might keep track of such a value internally, there's no standard way to get it.
In the past, Mozilla's Javascript variant exposed the non-standard
__count__
, but it has been removed with version 1.8.5.For cross-browser scripting you're stuck with explicitly iterating over the properties and checking
hasOwnProperty()
:In case of ECMAScript 5 capable implementations, this can also be written as (Kudos to Avi Flax)
Keep in mind that you'll also miss properties which aren't enumerable (eg an array's
length
).If you're using a framework like jQuery, Prototype, Mootools, $whatever-the-newest-hype, check if they come with their own collections API, which might be a better solution to your problem than using native JS objects.
要在任何 ES5 兼容环境中执行此操作
(来自此处的浏览器支持)
(关于 Object.keys 的文档此处,包括您可以添加的方法到非 ECMA5 浏览器)
To do this in any ES5-compatible environment
(Browser support from here)
(Doc on Object.keys here, includes method you can add to non-ECMA5 browsers)
如果您已经在构建中使用了 jQuery,只需执行以下操作:
它在对象上对我来说效果很好,并且我已经将 jQuery 作为依赖项。
if you are already using jQuery in your build just do this:
It works nicely for me on objects, and I already had jQuery as a dependancy.
AFAIK,除非切换到数组,否则没有办法可靠地做到这一点。 老实说,这似乎并不奇怪——数组是可数的,而对象不是,这对我来说似乎很简单。
也许你会得到的最接近的是这样的
但是这会产生问题,或者至少是问题。 所有用户创建的属性都会被计算在内,包括 _length 函数本身! 虽然在这个简单的示例中,您可以通过使用普通函数来避免它,但这并不意味着您可以阻止其他脚本执行此操作。 所以你会怎么做? 忽略函数属性?
最后,我认为你应该放弃让你的对象可数的想法,并找出另一种方法来完成你正在做的事情。
AFAIK, there is no way to do this reliably, unless you switch to an array. Which honestly, doesn't seem strange - it's seems pretty straight forward to me that arrays are countable, and objects aren't.
Probably the closest you'll get is something like this
But this creates problems, or at least questions. All user-created properties are counted, including the _length function itself! And while in this simple example you could avoid it by just using a normal function, that doesn't mean you can stop other scripts from doing this. so what do you do? Ignore function properties?
In the end, I think you should probably ditch the idea of making your objects countable and figure out another way to do whatever it is you're doing.
数字/长度/维数的概念对于对象来说并没有真正的意义,需要它表明你真的想要一个数组。
编辑:向我指出你想要 O(1) 。 据我所知,恐怕不存在这样的方法。
The concept of number/length/dimensionality doesn't really make sense for an Object, and needing it suggests you really want an Array to me.
Edit: Pointed out to me that you want an O(1) for this. To the best of my knowledge no such way exists I'm afraid.
使用 jquery :
With jquery :