Some functions of Nodejs Lodash

Source: Internet
Author: User
Tags compact

1 _.compact usage _.compact ([0, 1, False, 2, ', 3, ' mm ']);

var test = _.  Compact([ -1,0, 1, False, 2, ', 3, ' JJ ']);    Console.log (test);----1,1,2,3,jj//output  

2_.different usage

var test = _.difference ([1, 2, 3, 4, 5], [5, 2, ten]);    Console.log (test); 1,3,4//output Results  

3_.find usage

var characters = [  {' name ': ' Barney ', ' age ': $, '  blocked ': false},  {' name ': ' Fred ',    ' age ': +, ' Blocke d ': true},  {' name ': ' Pebbles ', ' age ': 1,  ' blocked ': false}];_.find (characters, function (CHR) {    console . log (chr.age);  return Chr.age < 40;}); Find
for (var n = 0; n < activities.length; n++) {         if (activities[n].name = = bidlist[0].activityname) {                   //(middle code omitted) middle The assumption is to change a property in the object of the activity found by the loop, such as  status, to change its value to 0;    }  Replace the _.find (activities,function (activity) {return activity.name = = Bidlist[0].activityname}) with the _.find method. Status = 0;  

4 Use Map to remove some for loops

var a = [0,1,2,3,4] for  (var i = 0;i < a.length;i++) {        a[i] = a[i]+1;  }  

Replace later

_.map (a,function (num) {        return num = num + 1;  })  

5 Replace for after replace if

var A;  if (activities.length) {        a = 1;  }  if (!activities.legth) {        a = 0;  }  

Replace later

Refactoring also has a lot of knowledge, now only use their own to comb them, and then continue to summarize.

The above describes the JavaScript Lodash usage summary, including the aspects of the content, I hope the JavaScript tutorial interested in a friend helpful.

Some functions of Nodejs Lodash

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.