Summary of common JavaScript lodash usage series, javascriptlodash
Lodash was originally a fork of the Underscore. js library, because it was different from other contributors (Underscore. js. John-David Dalton's initial goal was to provide more "consistent cross-browser behavior ......, And improve performance ". Later, the project achieved greater results on the basis of the existing success, and released version 3.0 in March.
The following describes the knowledge of javascript lodash. The details are as follows:
1 _. compact usage
_. Compact ([0, 1, false, 2, '', 3, 'mm']); var test = _. compact ([-1, 0, 1, false, 2, '', 3, 'JJ]); console. log (test); ----, jj // The output does not contain 0 false Spaces
2 _. different usage
Var test = _. difference ([1, 2, 3, 4, 5], [5, 2, 10]); console. log (test);, 4 // The output result is that the first array is output and the second array does not
3 _. find usage
Var characters = [{'name': 'Barney ', 'age': 36, 'blocked': false}, {'name': 'fred', 'age ': 40, 'blocked': true}, {'name': 'pebbles ', 'age': 1, 'blocked': false}]; _. find (characters, function (chr) {console. log (chr. age); return chr. age <40 ;}); search for (var n = 0; n <activities. length; n ++) {if (activities [n]. name = bidList [0]. activityName) {// (the intermediate code is omitted) The intermediate assumption is to modify an attribute of the activity object found in the loop, for example, status, and change its value to 0;} Use _. replace _. find (activities, function (activity) {return activity. name = bidList [0]. activityName }). status = 0; // then we can find the corresponding object through find and change the value.
4. Remove some for loops with map
var a = [0,1,2,3,4] for (var i = 0;i < a.length;i++){ a[i] = a[i]+1; }
After replacement
_.map(a,function(num){ return num = num + 1; })
5. replace if
var a; if(activities.length){ a = 1; } if(!activities.legth){ a = 0; }
After replacement
var a = activities.length ? 1 : 0;
The above is a summary of the common usage series of JavaScript lodash. I hope it will help you. If you have any questions, please leave a message and I will reply to you in a timely manner. Thank you very much for your support for the help House website!