Underscore.js

_.each(list, iteratee, [context]) 

The function of this function is: according to the filter conditions provided by the iterator iterator, traverse each element in the list list, and generate an iterative function each time it is traversed. In addition, the iterator iterator can also be bound to the optional context context object.
_.each([1, 2, 3], alert); 
=> 1; 2; 3 (Each element in the output array after operation, equivalent to a for loop, no return value.) 


_.map(list, iteratee, [context])  

The function of this function is: according to the function in the iterator iterator, traverse each element in the list list, after completing the traversal of the list elements, the function will return an array object.
_.map([1, 2, 3], function(num){ return num * 2; }); 
=> [2, 4, 6] (the output is the array after the operation, which is equivalent to the mapping, the new array The length is equal to the length of the original array, and there is a return value.)


_.reduce(list, iteratee, [memo], [context]) 
aliased to inject and foldl, the reduce method reduces the elements in the list to a single value. Memo is the initial value of the reduce function, and each step of the reduce needs to be returned by iteratee. This iteration is passed 4 parameters: memo, value and the index (or key) of the iteration and the entire list of the last reference. 
If no memo is passed to the initial call to reduce, iteratee will not be called with the first element in the list. The first element will replace the memo parameter passed to the next element in the list to call iteratee. 
var sum = _.reduce([1, 2, 3], function(memo, num){ return memo + num; }, 0); 
=> 6 (the output is the operation result of all elements of the traversed array, which is A value. There is a return value.) 

_.filter(list, predicate, [context])
traverses each value in the list and returns the value of all elements that pass the truth value of the predicate test. (Note from Fool's Dock: If there is a native filter method, use the native filter method.) 
var evens = _.filter([1, 2, 3, 4, 5, 6], function(num){ return num % 2 = = 0; }); 
=> [2, 4, 6] (The output is the array filtered by the condition, the length of the new array is less than or equal to the length of the original array, and there is a return value.) 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326524829&siteId=291194637