Slide Images

Friday 8 June 2018

A Quick Introduction to Lodash

https://upcity.com/wp-content/uploads/2018/06/lodash.jpg [ad_1]



In addition to guest posting on the UpCity blog, Sunlight Media is featured as one of the Top Software Developers in the United States. Check out their profile here.


In the world of JavaScript development, there is a near boundless amount of tools, libraries and frameworks that are released with ever-increasing frequency. While change happens rapidly, one tool that has had some staying power (albeit through a fair amount of re-iteration) is Lodash.


Originally appearing as Underscore.js, Lodash is a JavaScript utility library that offers extremely useful methods for arrays, objects, and functions — many of which (if not most of) are not available in vanilla JavaScript alone. Standardizing many of the most common methods used in a modern JavaScript application, Lodash can cut down on development time, make your codebase smaller, as well as create consistency across development teams. This post will give some background on Lodash, how to get started with it, as well as some practical examples of how it might be used in your code.


Origins


Originally released in 2009, Underscore.js became a popular early utility library for JavaScript, used in tandem with JS frameworks such as Backbone and Angular. As development of the library more or less came to a halt after a few years, the Lodash project was forked, becoming a superset of Underscore, adding far more helper functions and methods than were available initially. While Underscore is still available and used by some, Lodash has significantly higher popularity, due to its ongoing development and superior set of features.


Getting Started


To add Lodash to your project, you can download the core or full builds from its official site, install via npm (npm i —save lodash), or use the jsDelivr CDN.


One of the great things about Lodash is that it’s broken up into individual modules. You can choose to load the full build if you really need it, but if not, choosing to load only the modules you need (such as chain.js or forEach.js) will significantly cut down on your code base.


lodash


(Above: individual modules of Lodash available on the jsDelivr CDN)


Library Contents


The Lodash library is broken up into 13 main areas, with methods for an Array, Collection, Date, Function, Lang, Math, Number, Object and other areas. All methods start with an underscore _ (hence the library names “Underscore” / “Lodash”) followed by a period then a descriptor of what it does.


Below are just a few examples of useful methods available in the library:


_.chunk


_.chunk will take an array passed in as an argument and evenly “chunk” it into smaller groups, the length of which is given as a second argument. If the array isn’t evenly divisible, the last grouping will be the remaining leftover values:


_.chunk([2000, 2001, 2002, 2003, 2004, 2005], 2);
// returns [[2000, 2001], [2002, 2003], [2004, 2005]]

_.chunk([2000, 2001, 2002, 2003, 2004, 2005], 5);
// returns [[2000, 2001, 2002, 2003, 2004], [2005]]


_.tail


_.tail will return all but the first value of an array passed to it:


_.tail([2000, 2001, 2002, 2003]);
// returns [2001, 2002, 2003]


_.flattenDeep


._flattenDeep recursively “flattens” or removes all layers of nested arrays within an array:


_.flattenDeep([a, b, [c, d, [e, f]], g]);
// returns [a, b, c, d, e, f, g]


Why You Should Use Lodash


While some methods available in Lodash have since become available in newer specifications of JavaScript such as ECMAScript 6, 7 and beyond, many areas of the library’s functionality are simply not possible without considerable coding. Lodash does what it does best: creating building blocks for commonly needed methods and functions, in a way that is consistent across browsers and across development teams.







mabc-shadow-none mabc-border-thin mabc-bckg-none" style=" ;">







Sunlight Media headshot










Nicholas Morera










While always deeply interested in technology since childhood, Nicholas has been involved in web development in a professional capacity since 2012, as both a front-end developer and project manager. He is most adept at HTML, CSS & JavaScript, but is interested in the entire spectrum of computer science. Some of his tech interests include full-stack JavaScript development, Unix-based operating systems, open-source web projects, and computer-assisted composition. He is Senior Project Manager for LA-based web design and development agency, Sunlight Media.












[ad_2]

Read_more MMO mastermind

No comments: