Lo-Dash,数组和集合之间的区别

时间:2021-11-25 13:27:34

A glance at the Lo-Dash docs shows that the API falls in to categories of:

浏览一下Lo-Dash文档,可以看出API属于以下类别:

  1. Arrays,
  2. Chaining,
  3. Collections,
  4. Functions,
  5. Objects,
  6. Utilities,
  7. Methods,
  8. and Properties

A more detailed look in to the Arrays API shows approximately 30 different methods available that are applicable to arrays.

更详细地了解Arrays API,可以看到大约30种适用于数组的不同方法。

The Collections API has a few more methods than the Arrays API, and they do not share the same methods.

Collections API比Arrays API有更多的方法,它们不共享相同的方法。

Within the Collections API, a collection is described as an object that is iterated, and may be an array:

在Collections API中,集合被描述为迭代的对象,可以是一个数组:

collection (Array|Object|string): The collection to iterate over.

collection(Array | Object | string):要迭代的集合。

Also, interestingly, there's a Collections API method _.toArray that returns an array from a collection:

另外,有趣的是,有一个Collections API方法_.toArray从集合中返回一个数组:

Arguments

collection (Array|Object|string): The collection to convert. Returns

collection(Array | Object | string):要转换的集合。返回

(Array): Returns the new converted array.

(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.

有人会碰巧知道Lo-Dash API中数组和集合之间的形式差异吗?由于Backbone.js的原因,我认为这是一个差异,但是,现在我正在质疑我的推理,因为这些方法可能在其他地方可用。提前致谢。

2 个解决方案

#1


27  

It's a good idea to look at the more elaborate Underscore.js documentation, from which this distinction is derived. It states:

查看更精细的Underscore.js文档是一个好主意,从中可以得出这种区别。它指出:

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 numeric length property.

集合函数适用于数组,对象和类似数组的对象,如参数,NodeList和类似对象。但它通过duck-typing工作,因此避免传递具有数字长度属性的对象。

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.

基本上,“集合”是实现某种“可迭代”接口的东西,它们在内部使用相同的迭代方法(尽管Lodash源比Underscore更复杂)。所有“集合方法”都可以在数组和对象(以及一些可迭代的东西)上工作,而数组方法只能用于数组(或者可能是所有具有.length和数字索引的东西),并且对象方法可以工作任何物体。

#2


9  

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.

所有数组都是集合,但并非所有集合都是数组。对象(即{k:v,...})是不是数组的集合。许多迭代器可以很好地迭代非数组集合。在这种情况下,您可以将数组视为或多或少地由连续的非负整数索引的有序集合。

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.

函数得到的参数取决于你迭代的东西。如果你在一个数组上进行迭代,那么你将获得元素和索引,如果你在一个Object上进行迭代,那么你将获得值和键。

#1


27  

It's a good idea to look at the more elaborate Underscore.js documentation, from which this distinction is derived. It states:

查看更精细的Underscore.js文档是一个好主意,从中可以得出这种区别。它指出:

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 numeric length property.

集合函数适用于数组,对象和类似数组的对象,如参数,NodeList和类似对象。但它通过duck-typing工作,因此避免传递具有数字长度属性的对象。

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.

基本上,“集合”是实现某种“可迭代”接口的东西,它们在内部使用相同的迭代方法(尽管Lodash源比Underscore更复杂)。所有“集合方法”都可以在数组和对象(以及一些可迭代的东西)上工作,而数组方法只能用于数组(或者可能是所有具有.length和数字索引的东西),并且对象方法可以工作任何物体。

#2


9  

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.

所有数组都是集合,但并非所有集合都是数组。对象(即{k:v,...})是不是数组的集合。许多迭代器可以很好地迭代非数组集合。在这种情况下,您可以将数组视为或多或少地由连续的非负整数索引的有序集合。

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.

函数得到的参数取决于你迭代的东西。如果你在一个数组上进行迭代,那么你将获得元素和索引,如果你在一个Object上进行迭代,那么你将获得值和键。