Summary of common JavaScript lodash usage series, javascriptlodash

Source: Internet
Author: User

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!

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.