1. Installation
Node. js –> npm install underscore
Meteor.js –> meteor add underscore
Require.js –> require([“underscore”], …
Bower bower –> install underscore
Component –> component install jashkenas/underscore
Second, aggregate function (array or object)
1, each
Syntax: _.each(list, iteratee, [context])
Description: Traverse all elements in the list, and call the iteratee function with each element as a parameter in order. If the context parameter is passed, bind iteratee to the context object. Each call to iteratee passes three parameters: (element, index, list). If list is a JavaScript object, the arguments to iteratee are (value, key, list)). Return a list to facilitate chain calls.
Example:
_.each([1, 2, 3], alert);
=> alerts each number in turn…
_.each({one: 1, two: 2, three: 3}, alert);
=> alerts each number value in turn…
2, map
Syntax: _.map(list, iteratee, [context])
Description: Generate a corresponding array by calling the conversion function (iteratee iterator) for each element in the list. iteratee passes three parameters: value, then the iteration index (or key Fool’s Wharf note: if the list is a JavaScript object, this parameter is the key), and the last one is a reference to the entire list.
Example:
_.map([1, 2, 3], function(num){ return num * 3; });
=> [3, 6, 9]
_.map({one: 1, two: 2, three: 3}, function(num, key){ return num * 3; });
=> [3, 6, 9]
_.map([[1, 2], [3, 4]], _.first);
=> [1, 3]
3, reduce
Syntax: _.reduce(list, iteratee, [memo], [context])
Description: The aliases are inject and foldl, and the reduce method reduces the elements in the list to a single value. Memo is the initial value of the reduce function and will be replaced by the return value of each successful call to the iteratee function. This iteration is passed 4 parameters: memo, value and index (or key) of the iteration and the entire list referenced by the last one.
If no memo is passed to the initial call to reduce, iteratee will not be called on the first element in the list. The first element will be passed instead of the memo argument to the iteratee function called on the next element in the list.
Example:
var sum = _.reduce([1, 2, 3], function(memo, num){ return memo + num; }, 0);
=> 6
4, reduceRight
Syntax: _.reduceRight(list, iteratee, memo, [context])
Explanation: reduceRight is a reduce function that combines elements from the right. Foldr is not as useful in JavaScript as other languages with lazy evaluation (Fool’s Wharf Note: lazy evaluation: An evaluation strategy that evaluates an expression only when its value is actually required).
Example:
var list = [[0, 1], [2, 3], [4, 5]];
var flat = _.reduceRight(list, function(a, b) { return a.concat(b); }, []);
=> [4, 5, 2, 3, 0, 1]
// The concat() method is used to concatenate two or more arrays.
5, find
Syntax: _.find(list, predicate, [context])
Description: Search item by item in the list, return the first element value that passes the true value detection of the predicate iteration function, and return undefined if no element passes the detection. If a matching element is found, the function returns immediately without traversing the entire list.
&nbyle=”margin-left:0pt;”>};
var template = _.template(“Hello {
{ name }}!”);
template({name: “Mustache”});
=> “Hello Mustache!”
By default, the template gets all the values of data through the with statement. Of course, you can also specify a variable name in the variable setting. This can significantly improve the rendering speed of the template.
_.template(“Using ‘with’: “, {variable: ‘data’})({answer: ‘no’}) ;
=> “Using ‘with’: no”
Precompiled templates are helpful for debugging non-reproducible errors. This is because precompiled templates can provide error code line numbers and stack traces, and some templates are displayed on the client (Browser) cannot be compiled. On the compiled template function, there is a source attribute that can provide a simple pre-compilation function.
JST.project = ;
7. Chaining
You can use Underscore in an object-oriented or functional style, depending on your personal preference. The following two lines of code can multiply all numbers in an array by 2 .
_.map([1, 2, 3], function(n){ return n * 2; });
_([1, 2, 3]).map(function(n){ return n * 2; });
Using the chain method on an object will encapsulate the object and return the encapsulated object after each method call. When you complete the calculation, you can use value function to get the final value. The following is an example of a chain grammar using map/flatten/reduce at the same time, the purpose is to count the number of occurrences of each word in the lyrics of a song.
var lyrics = [
{line: 1, words: “I’m a lumberjack and I’m okay”},
{line: 2, words: “I sleep all night and I work all day”},
{line: 3, words: “He’s a lumberjack and he’s okay”},
{line: 4, words: “He sleeps all night and he works all day”}
];
_.chain(lyrics)
.map(function(line) { return line.words.split(‘ ‘); })
.flatten()
.reduce(function(counts, word) {
counts[word] = (counts[word] || 0) + 1;
return counts;
}, {})
.value();
=> {lumberjack: 2, all: 4, night: 2 … }
In addition, the array prototype method is also added to the chain-encapsulated Underscore object through a proxy, so you can directly use the reverse or push method in the chain syntax, and then Then follow the other sentences.
1, chain
Syntax: _.chain(obj)
Description: Returns an encapsulated object. Calling a method on the encapsulated object will return the encapsulated object itself until the value method is called.
Example:
var stooges = [{name: ‘curly’, age: 25}, {name: ‘moe’, age: 21}, {name: ‘larry’, age: 23}];
var youngest = _.chain(stooges)
.sortBy(function(stooge){ return stooge.age; })
.map(function(stooge){ return stooge.name + ‘ is ‘ + stooge.age; })
.first()
.value();
=> “moe is 21”
2, value
Syntax: _(obj).value()
Description: Get the final value of the encapsulated object.
Example:
_([1, 2, 3]).value();
=> [1, 2, 3]
tyle=”margin-left:0pt;”>Example:
_([1, 2, 3]).value();
=> [1, 2, 3]