Use of underscore library

The underscore.js library is a js utility library that provides a full set of functional programming utility functions, but does not extend any JavaScript built-in objects. This is the official explanation of the js library. In my opinion, the js library is equivalent to defining a "_" object, which has many methods. More than 100 functions are provided in 1.8.3. If you want to use this library, the first thing you need to do is: install this library in your browser. When I use this library, I use more: each, map, filter, contains.
each
_.each(list, iteratee, [context]) 

In my use process, it is somewhat similar to for, traversing all elements in the list, and traversing each element in order.
If the context parameter is passed, iteratee is bound to the context object.
var number = [0,1,9,3,0,8,7];
_.each(number,console.log);

The result is:
0
1
9
3
0
8
7
In some cases it can be used instead of for, and it is simpler than for. If the above program uses for to traverse, it will require at least four lines of code. The role of the library can be well reflected at this time.
_.map(list, iteratee, [context])

Mapping each value in the list through a transformation function (iteratee iterator) yields a new array of values.
var number = [0,1,9,3,0,8,7];
var number_1 = _.map(number,function(num){
    return num * 3;
})
console.log(number_1);




_.filter(list, predicate, [context]) 

Iterates over each value in the list and returns the value of all elements that pass the truth value of the predicate test. (This one has a return value).
var evens = _.filter ([1, 2, 3, 4, 5, 6], function (num) {
return num % 2 == 1;
});
console.log(evens)

The output is: 1,3,5
Remember that this method has a return value. . . . .

_.contains(list, value, [fromIndex]) 

Returns true if the list contains the specified value, which is used to determine whether the substring or subobject has a good effect on the parent string or wood object.
E.g:
var number = [1,2,3,0,9,8]
if(_.contains(number,3)){
     number[2] = 1;
}
console.log(number[2])
          









Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=326858326&siteId=291194637