Recent project needs, began to learn underscore.js, on the CSS88 website to see the Chinese API, while watching to record their understandable notes;
The following, functions and partial explanations are copied from css88 Fool wharf Chinese API
The content is still being sorted out, the issue part
/***@ Method parameter interpretation *iterator as an iterator, the function will get the previous array or the value of the object as a parameter passed in, and processing * predicate for truth detection, the function will be the previous array or object value as a parameter passed, and according to the function content to judge, Returns true if it matches, otherwise the false* @ function parameter is interpreted * The list array or the object itself is a part of the properties object, which is generally used to determine whether the previous object or array contains that part of the content * Memo initial value, is empty; the function used has reduce, Reduceright, which is used for iterator functions (iterator) * When an element function is used for an array, a subkey of the exponential group is used, and more is used for the iterator function (iterator) * When the index function uses an array of objects, the parameter is used. Refers to the arrangement of the used children (in fact the name is very obvious), more for the iterator function (iterator) * The value function uses the object as an object, the function is used to refer to the value of one of the object's properties, more for the iterator function (iterator) * Key function when using object as Object , this function is used, which is a property of an object, and is used more as an iterator function (iterator)*//*aggregate Functions*//** * Each function _.each (list, iterator, [context]) * Each function iterates over the previous parameter * if it is an array, the parameters of the subsequent function are element (array contents), index (corresponding sort number), List (the array itself) * If it is an object, the parameters of the subsequent function are value (values), key (key), list (the object itself) * num @type {number}*/varnum = 0;varEachresult = _.each ([1, 2, 3],function(ele,index,list) {//num + = ele;//alert (ele);//alert (index);}); _.each ({one:1, Two:2, three:3},function(val,key) {//Console.log (key+ ":" +val);});/** * Map function. Map (list, iterator, [context]) * Map function maps each element (or value) in a list to a new array (automatically builds a new array) using an iterator * Array when the function argument is a element,index,list* object, the function parameter is value,key,list*/_.map ([1, 2, 3],function(ele,index,list) {//Console.log (ele);//Console.log (index);//Console.log (list);returnEle * 3;}); _.map ({one:1, Two:2, three:3},function(value, key,list) {returnValue * 3;});/*the Reduce function _.reduce (list, iterator, memo, [context]) * puts each element in the collection into an iterative processor and passes the return value of this iteration as "Memo" to the next iteration, typically for cumulative results or connection data * Array when parameter is memo,ele,index,list* object when parameter is Memo,value,key,list*/_.reduce ([1, 2, 3],function(Memo, num) {//return memo + num;}, 0);//Console.log (flat);/*reduceright function _.reduceright (list, iterator, memo, [context]) * Like the reduce function, just order from right to left*/varlist = [[0, 1], [2, 3], [4, 5]];//concat linked ArraysvarFlat = _.reduceright (list,function(A, B) {returnA.concat (b);}, []);/** * Find function _.find (list, predicate, [context]) * Find the contents of the array that can be matched by a function, return the first number and understand the End Function * Find function argument is list (array), function*/vareven = _.find ([1, 2, 3, 4, 5, 6],function(num) {returnNum% 2 = = 0; });//=>2 End Function immediately after returning 2/***filter function _.filter (list, predicate, [context]) * Similar to the Find function, the difference is that it returns all the values that can pass the function and makes up a new array to return.*/varEvens = _.filter ([1, 2, 3, 4, 5, 6],function(num) {returnNum% 2 = = 0; });//= = [2, 4, 6] Returns a new function/** * Where function _.where (list, properties) * parameter is list, properties (object) * Returns data containing the Properties section in list*/listofplays= [{title: "Cymbeline", Author: "Shakespeare", year:1611},{title:"The Tempest", Author: "Shakespeare", Year:1611},{title: "The Tempest", Author: "One by one", year:1611}];varwhere = _.where (Listofplays, {author: "Shakespeare", year:1611});//Console.log (where);/*The **findwhere function _.findwhere (list, properties) * parameter is list, properties (object) * Iterates through each value in the list, returning all keys that match properties listed in Property- The first value of a value pair. * If no matching attribute is found, or the list is empty, then undefined will be returned. */varpublicservicepulitzers= [{year:1918, Newsroom: "The New York Times", Reason:"For it public service in publishing in full so many official reports,documents and speeches by European statesmen Relati Ng to the progress andconduct of the war. "}];_.findwhere (publicservicepulitzers, {newsroom:"The New York Times"});/*= = {year:1918, newsroom: "The New York Times", Reason: "For it public service in publishing in full so many Offici Al Reports,documents and speeches by European statesmen relating to the progress andconduct of the War. "}*//***reject function _.reject (list, predicate, [context]) * Returns a collection of elements that are not detected by the predicate truth value, as opposed to the filter * parameter is list,predicate*/varOdds = _.reject ([1, 2, 3, 4, 5, 6],function(num) {returnNum% 2 = = 0; });//= = [1, 3, 5] does not form a new array of detected data to return/** * Every function _.every (list, [predicate], [context]) * Returns true* if all elements in the list can be detected by predicate true truth Return the function of detecting truth value with return in predicate function*/varResult =_.every ([1,2,3,4,5],function(ele,index,list) {//Console.log (ele);//Console.log (index);//Console.log (list);returnEle >=1;});//Console.log (result);/** * Some function _.some (list, [predicate], [context]) * Returns true if any of the elements in the list are detected by predicate truth. Once a qualifying element is found, the traversal of the list is interrupted directly.*/varSomeresult = _.some ([NULL, 0, ' yes ',false],function(ele,index,list) {//Console.log (ele);//Console.log (index);//Console.log (list);returnEle >=1;});//Console.log (someresult);/** * Contains_.contains (list, value) * Returns True if the list contains the specified value (Fool's Wharf Note: use = = = detection). If the list is an array, the internal use of indexof is judged. */varCONTAINSRESULT1 = _.contains ([1,2,3],2);varCONTAINSRESULT2 = _.contains ({title:1,name:2,age:3},2);//Console.log (CONTAINSRESULT1);//Console.log (CONTAINSRESULT2);//Todo does not understand where MethodName came from, currently understood to get the same name function and pass parameters in/** * The Invoke function _.invoke (list, methodName, *arguments) * Executes the MethodName method on each element of the list. Any additional arguments passed to invoke, invoke is passed to it when the MethodName method is called. */varInvokeresult = _.invoke ([[5, 1, 7], [3, 2, 1]], ' sort ');//Console.log (invokeresult);/** * Pluck _.pluck (list, PropertyName) * Gets an attribute in an object array, gets all the values of the property and forms a new array to return*/varPluckstooges = [{name: ' Moe ', age:40}, {name: ' Larry ', age:50}, {name: ' Curly ', age:60}];_.pluck (Pluckstooges,' Name ');//= = ["Moe", "Larry", "Curly"]/** * Max _.max (list, [iterator], [context]) * Returns the maximum value in the list. If you pass the iterator parameter, iterator will be the basis for the list sort. * The following example: The basis for judging the maximum value is Stooge.age, written in the iterator function, the principle is that the value of the property is sorted, the maximum value is fetched and the object*/varMaxstooges = [{name: ' Moe ', age:40}, {name: ' Larry ', age:50}, {name: ' Curly ', age:60}];_.max (Maxstooges,function(stooge) {returnstooge.age;});//= = {Name: ' Curly ', age:60};/** * min _.min (list, [iterator], [context])*/
Underscore.js Study Notes