any method:
It's similar to all method, so I won't go into details, take a look at the example
[].any()
// -> false (empty arrays have no elements that could be true-equivalent)
$R(0, 2).any()
// -> true (on the second loop cycle, 1 is true-equivalent)
[2, 4, 6, 8, 10].any(function(n) { return 0 == n % 3; })
// -> true (the iterator will return true on 6: the array does have 1+ multiple of 3)
$H({ opt1: null, opt2: false, opt3: '', opt4: 'pfew!' }).any(function(pair) { return ; })
// -> true (thanks to the opt4/'pfew!' pair, whose value is true-equivalent)
collect/map (collect method alias) method:
Returns the results of applying the iterator to each element. Aliased as map.
This is a sort of Swiss-Army knife for sequences. You can turn the original values into virtually anything!
This method is called "Swiss Army Knife". This method can basically perform any operation on data. The map method is an alias for this method. The implementation of this method is very simple. ((context, value, index)); This sentence is the key, which is to make an iterator call on each data and store the result in the array to be returned. Take a look at the example:
['Hitch', "Hiker's", 'Guide', 'To', 'The', 'Galaxy'].collect(function(s) { return (0).toUpperCase(); }).join('')
// -> 'HHGTTG'
$R(1,5).collect(function(n) { return n * n; })
// -> [1, 4, 9, 16, 25]
Note that there are several lines of prompts at the end of the help document:
First, the method-calling scenario: you want to invoke the same method on all elements, possibly with arguments, and use the result values. This can be achieved easily with invoke.
Second, the property-fetching scenario: you want to fetch the same property on all elements, and use those. This is a breeze with pluck.
The detect method:
Find the first data that meets a certain condition and return it, and look at the example. In fact, this method is the alias of find, so calling detect and find are the same:
// An optimal exact prime detection method, slightly compacted.
function isPrime(n) {
if (2 > n) return false;
if (0 == n % 2) return (2 == n);
for (var index = 3; n / index > index; index += 2)
if (0 == n % index) return false;
return true;
}
// isPrime
$R(10,15).find(isPrime) // -> 11
[ 'hello', 'world', 'this', 'is', 'nice'].find(function(s) { return <= 3; })
// -> 'is'
Each method:
When calling this method, it is actually an iterator operation on each data. The first parameter of the iterator function is data and the second parameter is index. During the traversal process, $continue and $break exceptions can be thrown. Note:
The usage of $continue
is deprecated. This feature will not be available in releases after Prototype 1.5 in favor of speed.
Take a look at the example:
['one', 'two', 'three'].each(function(s) { alert(s); });
[ 'hello', 'world'].each(function(s, index) { alert(index + ': ' + s); });
// alerts -> '0: hello' then '1: world'
// This could be done better with an accumulator using inject, but humor me
// here...
var result = [];
$R(1,10).each(function(n) {
if (0 == n % 2) throw $continue;
if (n > 6) throw $break;
(n);
});
// result -> [1, 3, 5]
EachSlice method:
The following are many simple methods to give examples directly. I will not explain them anymore. There is nothing rare about the algorithm. I can basically understand it at a glance:
var students = [ { name: 'Sunny', age: 20 }, { name: 'Audrey', age: 21 }, { name: 'Matt', age: 20 }, { name: 'Élodie', age: 26 }, { name: 'Will', age: 21 }, { name: 'David', age: 23 }, { name: 'Julien', age: 22 }, { name: 'Thomas', age: 21 }, { name: 'Serpil', age: 22 } ];
(4, function(toon) { return ('name'); })
// -> [ ['Sunny', 'Audrey', 'Matt', 'Élodie'],
// ['Will', 'David', 'Julien', 'Thomas'],
// ['Serpil'] ]
//The following first method is provided by the Array object
(2).first()
// -> [{ name: 'Sunny', age: 20 }, { name: 'Audrey', age: 21 }]
The entries method is the toArray method. The map method is called in the toArray method. The map method is equivalent to the collect method:
$R(1, 5).toArray() // -> [1, 2, 3, 4, 5]
find/findAll (alias for select method) method:
// Find method
[ 'hello', 'world', 'this', 'is', 'nice'].find(function(s) { return <= 3; })
// -> 'is'
//findAll method
$R(1, 10).findAll(function(n) { return 0 == n % 2; })
// -> [2, 4, 6, 8, 10] [ 'hello', 'world', 'this', 'is', 'nice'].findAll(function(s) { return >= 5; })
// -> ['hello', 'world']
grep method:
What you need to note in this method is that the parameter filter should be unified into a regular expression in the function, and then the match method of the regular expression is called to make judgments
// Get all strings with a repeated letter somewhere
['hello', 'world', 'this', 'is', 'cool'].grep(/(.)\1/)
// -> ['hello', 'cool']
// Get all numbers ending with 0 or 5
$R(1,30).grep(/[05]$/)
// -> [5, 10, 15, 20, 25, 30]
// Those, minus 1
$R(1,30).grep(/[05]$/, function(n) { return n - 1; })
// -> [4, 9, 14, 19, 24, 29]
// Get all strings with a repeated letter somewhere
['hello', 'world', 'this', 'is', 'cool'].grep(/(.)\1/)
// -> ['hello', 'cool']
// Get all numbers ending with 0 or 5
$R(1,30).grep(/[05]$/)
// -> [5, 10, 15, 20, 25, 30]
// Those, minus 1
$R(1,30).grep(/[05]$/, function(n) { return n - 1; })
// -> [4, 9, 14, 19, 24, 29]
inGroupsOf method:
var students = [ { name: 'Sunny', age: 20 }, { name: 'Audrey', age: 21 }, { name: 'Matt', age: 20 }, { name: 'Élodie', age: 26 }, { name: 'Will', age: 21 }, { name: 'David', age: 23 }, { name: 'Julien', age: 22 }, { name: 'Thomas', age: 21 }, { name: 'Serpil', age: 22 } ];
//The pluck method is to obtain a certain property of the object, and the name property is obtained here
('name').inGroupsOf(4)
// -> [ ['Sunny', 'Audrey', 'Matt', 'Élodie'],
// ['Will', 'David', 'Julien', 'Thomas'],
// ['Serpil', null, null, null] ]
Include/member (alias for the include method) method, here we first check whether there is an indexOf method on the object. If so, call this method directly:
$R(1,15).include(10)
// -> true
['hello', 'world'].include('HELLO')
// -> false
[1, 2, '3', '4', '5'].include(3)
// -> true (== ignores actual type)
Inject method:
$R(1,10).inject(0, function(acc, n) { return acc + n; })
// -> 55 (sum of 1 to 10)
$R(2,5).inject(1, function(acc, n) { return acc * n; })
// -> 120 (factorial 5)
['hello', 'world', 'this', 'is', 'nice'].inject([], function(array, value, index) {
if (0 == index % 2) (value);
return array;
})
// -> ['hello', 'this', 'nice']
Invoke method:
['hello', 'world', 'cool!'].invoke('toUpperCase')
// ['HELLO', 'WORLD', 'COOL!']
['hello', 'world', 'cool!'].invoke('substring', 0, 3)
// ['hel', 'wor', 'coo']
max/min method:
$R(1,10).max() // -> 10
['hello', 'world', 'gizmo'].max()
// -> 'world'
function Person(name, age) { = name; = age; }
var john = new Person('John', 20);
var mark = new Person('Mark', 35);
var daisy = new Person('Daisy', 22);
[john, mark, daisy].max(function(person) { return ; })
// -> 35
Partition method:
['hello', null, 42, false, true, , 17].partition()
// -> [['hello', 42, true, 17], [null, false, undefined]]
$R(1, 10).partition(function(n) { return 0 == n % 2; })
// -> [[2, 4, 6, 8, 10], [1, 3, 5, 7, 9]]
['hello', null, 42, false, true, , 17].partition()
// -> [['hello', 42, true, 17], [null, false, undefined]]
$R(1, 10).partition(function(n) { return 0 == n % 2; })
// -> [[2, 4, 6, 8, 10], [1, 3, 5, 7, 9]]
Pluck method:
['hello', 'world', 'this', 'is', 'nice'].pluck('length')
// -> [5, 5, 4, 3, 4]
reject method:
$R(1, 10).reject(function(n) { return 0 == n % 2; })
// -> [1, 3, 5, 7, 9]
[ 'hello', 'world', 'this', 'is', 'nice'].reject(function(s) { return >= 5; })
// -> ['this', 'is', 'nice']
$R(1, 10).reject(function(n) { return 0 == n % 2; })
// -> [1, 3, 5, 7, 9]
[ 'hello', 'world', 'this', 'is', 'nice'].reject(function(s) { return >= 5; })
// -> ['this', 'is', 'nice']
The size method is omitted.
sortBy method:
This method first returns an object array through the map method, then calls the sort method of the array, and finally takes out the value attribute in the object
['hello', 'world', 'this', 'is', 'nice'].sortBy(function(s) { return ; })
// -> 'is', 'this', 'nice', 'hello', 'world']
['hello', 'world', 'this', 'is', 'cool'].sortBy(function(s) {
var md = (/[aeiouy]/g);
return null == md ? 0 : ;
})
// -> [ 'world', 'this', 'is', 'hello', 'cool'] (sorted by vowel count)
zip method:
var firstNames = ['Justin', 'Mislav', 'Tobie', 'Christophe'];
var lastNames = ['Palmer', 'Marohnić', 'Langel', 'Porteneuve'];
(lastNames)
// -> [['Justin', 'Palmer'], ['Mislav', 'Marohnić'], ['Tobie', 'Langel'], ['Christophe', 'Porteneuve']]
// Through this example, we can see that parameter a is an array, representing the corresponding terms in two arrays
(lastNames, function(a) { return (' '); })
// -> ['Justin Palmer', 'Mislav Marohnić', 'Tobie Langel', 'Christophe Porteneuve']
// Through this example, we can see that the incoming can be multiple arrays
var cities = ['Memphis', 'Zagreb', 'Montreal', 'Paris'];
(lastNames, cities, function(p) { return p[0] + ' ' + p[1] + ', ' + p[2]; })
// -> ['Justin Palmer, Memphis', 'Mislav Marohnić, Zagreb', 'Tobie Langel, Montreal', 'Christophe Porteneuve, Paris']
($R(1, 100), function(a) { return ().join('. '); })
// -> ['1. Justin', '2. Mislav', '3. Tobie', '4. Christophe']