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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/23921647/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-28 01:48:48  来源:igfitidea点击:

Lo-Dash, difference between array and collection

javascriptbackbone.jslodash

提问by aug2uag

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
  1. 数组,
  2. 链式,
  3. 收藏,
  4. 职能,
  5. 对象,
  6. 公用事业,
  7. 方法,
  8. 和属性

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.

集合(数组|对象|字符串):要迭代的集合。

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

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

Arguments

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

(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 造成的差异,但是,现在我质疑我为此的推理,因为这些方法可能在其他地方可用。提前致谢。

回答by Bergi

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

集合函数适用于数组、对象arguments和类似数组的对象,例如、NodeList 等。但它通过鸭子类型工作,因此避免传递具有数字length属性的对象。

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 .lengthand numeric indices), and the object methods work on any objects.

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

回答by mu is too short

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/

演示: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.

函数获得的参数取决于您迭代的对象类型。如果您正在迭代一个数组,那么您将获得元素和索引,如果您正在迭代一个对象,那么您将获得值和键。