Lodash of the JavaScript tool library

Source: Internet
Author: User

Are you still worrying about converting, matching, and finding data in JavaScript? A bunch of seemingly simple foreach, but long boring, but still keep repeat it! Perhaps you have already used underscore.js, good, you have progressed very big step. But today I want you to take a step further and replace underscore with Lodash.

Lodash started as a fork in the Underscore.js library because of disagreement with other contributors (underscore.js). John-david Dalton's initial goal was to provide more "consistent cross-browser behavior ... and improve performance". After that, the project achieved greater results on the basis of existing successes. Recently Lodash also released version 3.5, becoming the most dependent library in the NPM package repository. It is getting rid of the identity of the dick and becoming one of the developers ' regular choices.

Many of the open source projects we now know have been used or moved to the Lodash camp. such as JavaScript translator Babel, blog Platform Ghost, and Project scaffolding tool Yeoman. "This is a very sensible choice, and it's almost entirely driven by our open-source development community," said underscore, the founder of Lodash,ghost, who moved to the founding of John O ' Nolan. We found that Lodash contains more features, better performance, a good use of semver, and a growing eye-catching in the node. JS community (and other dependencies).

Lodash Walkthrough

Lodash mainly uses the delay calculation, which makes the lodash performance far more than underscore. Delaying computation in Lodash means that our chained method will not execute until the display or implicit value () is called. Because of this delay in execution, Lodash can perform optimizations such as Shortcut fusion, which significantly reduces the number of iterations by merging chained iteratee. The performance of its execution is greatly provided.

Hundred say better than a practice, below we take user information as an example:

var users = [  ' user ': ' Barney ',  ' age ': ' $ ' }  ,' user ': ' Fred ',    ' age ' : ,  ' user ': ' Pebbles ', ' age ': + }];
1. Get all user names and split with ","
var names = _.chain (users)  . Map (function(user) {    return  user.user;  })  . Join (",")  . Value (); Console.log (names);

Individuals prefer the realistic value of lodash delay calculations, as well as the functional style of JavaScript. In this case, the users object is first wrapped into a Lodash object, the map gets the names of all the users, and finally joins the user name with "," by using join. Note that this is just a string of method chains, and if you do not explicitly call the value method to make it perform immediately, you will get the following lodashwrapper delay expression:

true function function...}

Because of the existence of deferred expressions, we can increase the method chain multiple times, but this is not performed, so there is no performance problem, and finally we know that we need to use value to explicitly execute immediately.

2. Get the youngest user
var youngest = _.chain (users)  . Min (function(user) {    return user.age;  })  . Value (); Console.log (youngest);

This makes use of the Min function provided by Lodash to be easily solved.

Here bloggers also want to explain the optimization of the Lodash method chain in another way, the above method can be equivalent to the following way, the first user in the order of age:

var youngest2 = _.chain (users).  SortBy ("Age")  . Map (function(user) {    Console.log ("map", user);     return user;  })  . First ()  . Value (); Console.log (YOUNGEST2);

Here the blogger added a map as the log output, if you execute this line of code, you will be surprised to see that there will only be a user output, this can prove that in the immediate execution of the Lodash for our method chain to do a reliable optimization If we remove the first function you will see the output of 3 user objects.

3. Get the oldest user
var oldest  = _.chain (users)  . Max (function(user) {     return user.age;  })  . Value (); Console.log (oldest);

The Max function of Lodash is used here.

4. Conversion of user arrays to user map

In the development we often have to convert a bunch of data in the form of a group of attributes into an array of object form, easy to find based on the attribute key value, the following will be shown with the user object:

var userobj = _.chain (users)  . Map (function(user) {    return  [User.user, User.age];  })  . Zipobject ()  . Value (); Console.log (userobj);

Using Lodash, we first use the user array map as a collection of [key, value] arrays, and finally use Zipobject to convert the results to object objects, Zipobject takes the first item of the result set as the key and the second item as value to produce the object.

End

Here we show a small portion of the API in Knowledge Lodash, as the essay begins: Lodash is designed to provide more "consistent cross-browser behavior ... and improved performance" API. All the Lodash APIs you can find here https://lodash.com/docs#matches.

This article demonstrates the demo, you can also http://jsbin.com/xocixubaru/1/edit?html,js,output demo in Jsbin

Excerpt from: http://www.cnblogs.com/whitewolf/p/4417873.html

Lodash of the JavaScript ToolPak

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.