admin 管理员组文章数量: 1086019
A glance at the Lo-Dash docs shows that the API falls in to categories of:
- Arrays,
- Chaining,
- Collections,
- Functions,
- Objects,
- Utilities,
- Methods,
- and Properties
A more detailed look in to the Arrays API shows approximately 30 different methods available that are applicable to arrays.
The Collections API has a few more methods than the Arrays API, and they do not share the same methods.
Within the Collections API, a collection is described as an object that is iterated, and may be an array:
collection (Array|Object|string): The collection to iterate over.
Also, interestingly, there's a Collections API method _.toArray
that returns an array from a collection:
Arguments
collection (Array|Object|string): The collection to convert. Returns
(Array): Returns the new converted array.
Would anyone happen to know a formal difference between an array and collection in the Lo-Dash API? I was under the presumption it was a difference due to Backbone.js, however, am now questioning my reasoning to that end, since the methods may be available elsewhere. Thanks in advance.
A glance at the Lo-Dash docs shows that the API falls in to categories of:
- Arrays,
- Chaining,
- Collections,
- Functions,
- Objects,
- Utilities,
- Methods,
- and Properties
A more detailed look in to the Arrays API shows approximately 30 different methods available that are applicable to arrays.
The Collections API has a few more methods than the Arrays API, and they do not share the same methods.
Within the Collections API, a collection is described as an object that is iterated, and may be an array:
collection (Array|Object|string): The collection to iterate over.
Also, interestingly, there's a Collections API method _.toArray
that returns an array from a collection:
Arguments
collection (Array|Object|string): The collection to convert. Returns
(Array): Returns the new converted array.
Would anyone happen to know a formal difference between an array and collection in the Lo-Dash API? I was under the presumption it was a difference due to Backbone.js, however, am now questioning my reasoning to that end, since the methods may be available elsewhere. Thanks in advance.
Share Improve this question asked May 28, 2014 at 20:52 aug2uagaug2uag 3,4453 gold badges33 silver badges54 bronze badges2 Answers
Reset to default 44It's a good idea to look at the more elaborate Underscore.js documentation, from which this distinction is derived. It states:
Collection functions work on arrays, objects, and array-like objects such as
arguments
, NodeList and similar. But it works by duck-typing, so avoid passing objects with a numericlength
property.
Basically, "collections" are things that implement some kind of "iterable" interface, and they internally use the same iteration method (though Lodash source is a bit more convoluted than Underscore). All the "collection methods" do work both on arrays and objects (and a few more iterable things), while the array methods should only be used on arrays (or maybe everything with .length
and numeric indices), and the object methods work on any objects.
All Arrays are collections but not all collections are arrays. An Object (i.e. {k: v, ... }
) is a collection that is not an Array. Many of the iterators can iterate over non-Array collections just fine. In this context you can think of arrays as, more or less, ordered collections that are indexed by consecutive non-negative integers.
For example, both of these work:
_([6, 11, 23]).each(function() {
console.log(arguments);
});
_({ a: 6, b: 11, c: 23 }).each(function() {
console.log(arguments);
});
Demo: http://jsfiddle.net/ambiguous/t8a83/
The arguments that the function gets depend on what sort of thing you're iterating over. If you're iterating over an array then you'd get the element and the index, if you're iterating over an Object then you'd get the value and key.
本文标签: javascriptLoDash difference between array and collectionStack Overflow
版权声明:本文标题:javascript - Lo-Dash, difference between array and collection - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://roclinux.cn/p/1737381804a1827744.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论