📅  最后修改于: 2020-10-21 08:46:30             🧑  作者: Mango
Enumerable类为枚举提供了大量有用的方法。枚举是充当值集合的对象。
枚举方法主要用于枚举数组和哈希。还有其他对象,例如ObjectRange和各种与DOM或AJAX相关的对象,您可以在其中使用枚举方法。
每个带有迭代器的Enumerable方法都将上下文对象作为下一个(可选)参数。上下文对象是迭代器要绑定的对象,因此其内部的this关键字将指向该对象。
var myObject = {};
['foo', 'bar', 'baz'].each(function(name, index) {
this[name] = index;
}, myObject); // we have specified the context
myObject;
这将产生以下结果-
{ foo: 0, bar: 1, baz: 2}
这是与Enumerable相关的所有方法的完整列表。
注意-确保您至少具有1.6版的prototype.js。
S.No. | Method & Description |
---|---|
1. | all()
Determines whether all the elements are boolean-equivalent to true, either directly or through computation by the provided iterator. |
2. | any()
Determines whether at least one element is boolean-equivalent to true, either directly or through computation by the provided iterator. |
3. | collect()
Returns the results of applying the iterator to each element. Aliased as map(). |
4. | detect()
Finds the first element for which the iterator returns true. Aliased by the find() method. |
5. | each()
It lets you iterate over all the elements in a generic fashion, then returns the Enumerable, thereby allowing chain-calling. |
6. | eachSlice()
Groups items in chunks based on a given size, with last chunk being possibly smaller. |
7. | entries()
Alias for the more generic toArray method. |
8. | find()
Finds the first element for which the iterator returns true. Convenience alias for detect(). |
9. | findAll()
Returns all the elements for which the iterator returned true. Aliased as select(). |
10. | grep()
Returns all the elements that match the filter. If an iterator is provided, it is used to produce the returned value for each selected element. |
11. | inGroupsOf()
Groups items in fixed-size chunks, using a specific value to fill up the last chunk if necessary. |
12. | include()
Determines whether a given object is in the Enumerable or not, based on the == comparison operator. Aliased as member(). |
13. | inject()
Incrementally builds a result value based on the successive results of the iterator. |
14. | invoke()
Optimization for a common use-case of each() or collect(): invoking the same method, with the same potential arguments, for all the elements. |
15. | map()
Returns the results of applying the iterator to each element. Convenience alias for collect(). |
16. | max()
Returns the maximum element (or element-based computation), or undefined if the enumeration is empty. Elements are either compared directly, or by first applying the iterator and comparing returned values. |
17. | member()
Determines whether a given object is in the Enumerable or not, based on the == comparison operator. Convenience alias for include(). |
18. | min()
Returns the minimum element (or element-based computation), or undefined if the enumeration is empty. Elements are either compared directly, or by first applying the iterator and comparing returned values. |
19. | partition()
Partitions the elements in two groups: those regarded as true, and those considered false. |
20. | pluck()
Optimization for a common use-case of collect(): fetching the same property for all the elements. Returns the property values. |
21. | reject()
Returns all the elements for which the iterator returned false. |
22. | select()
Alias for the findAll() method. |
23. | size()
Returns the size of the enumeration. |
24. | sortBy()
Provides a custom-sorted view of the elements based on the criteria computed, for each element, by the iterator. |
25. | toArray()
Returns an Array representation of the enumeration. Aliased as entries(). |
26. | zip()
Zips together (think of the zip on a pair of trousers) 2 + sequences, providing an array of tuples. Each tuple contains one value per original sequence. |