Introduction
JavaScript is a language of Web. This series of articles will talk about my observations learned during my decade of software development experience with JavaScript.
Before moving further let us look at the previous articles of the JavaScript series:
- Voice of a Developer: JavaScript Data Types - Part One
- Voice of a Developer: JavaScript Objects - Part Two
- Voice of a Developer: JavaScript Engines - Part Three
- Voice of a Developer: JavaScript Common Mistakes - Part Four
- Voice of a Developer: Editors - Part Five
- Voice of a Developer: VSCode - Part Six
- Voice of a Developer: Debugging Capabilities of VSCode - Part Seven
- Voice of a Developer: JavaScript OOP - Part Eight
- Voice of a Developer: JavaScript Useful Reserved Keywords - Part Nine
- Voice of a Developer: JavaScript Functions - Part Ten
- Voice of a Developer: JavaScript Functions Invocations - Part Eleven
- Voice of a Developer: JavaScript Anonymous Functions - Part Twelve
- Voice of a Developer: JavaScript Pure And Impure Function - Part Thirteen
- Voice of a Developer: JavaScript Closures - Part Fourteen
- Voice of a Developer: JavaScript Currying - Part Fifteen
- Voice of a Developer: JavaScript Chaining - Part Sixteen
- Voice of a Developer: JavaScript Array Methods - Part Seventeen
- Voice of a Developer: JavaScript ECMAScript 6 - Part Eighteen
- Voice of a Developer: JavaScript ECMAScript 6 Features v1 - Part Nineteen
- Voice of a Developer: JavaScript ECMAScript 6 Features v2 - Part Twenty
- Voice of a Developer: JavaScript Web Workers - Part Twenty One
- Voice of a Developer: JavaScript JSLint v1 - Part Twenty-Two
- Voice of a Developer: JavaScript JSLint v2 - Part Twenty Three
- Voice of a Developer: XMLHttpRequest API - Part Twenty Four
- Voice of a Developer: Web Storage API - Part 25
- Voice of a Developer: Application Cache API - Part Twenty-Six
- Voice of a Developer: Javascript WebSocket - Part 27
- Voice of a Developer: Parse JSON - Part 28
In this article, we will explore functional programming aspects of JavaScript using a popular library called Loadash.js. It makes JavaScript easier. I use Lodash.js as Swiss Army knife to help me in breaking complex problems into smaller chunks and faster delivery.
In day-to-day programming, we work on solving business problems and working with arrays, numbers, objects, strings, etc. Loadash.js works great with these:
JavaScript
- Arrays, objects & strings
- Testing values
- Creating composite functions
This library is released with MIT license Download.
Note: I am going to refer to 4.12 of this library.
Use cases
I will demonstrate benefit of using Lodash.js to simply common problems. It also enhance the developer experience to understand and read code better.
Case 1: Sort an array and get unique values.
Naive method
- varary = [2, 4, 2, 5, 4, 7];
- varsortAry = ary.sort(); // [2, 2, 4, 4, 5, 7]
- //run two loops to traverse and match each element, e.g., 2 ===2; 2===4
- for (vari = 0; i < sortAry.length; i++)
- {
- for (var j = i + 1; j < sortAry.length; j++)
- {
- if (sortAry[i] === sortAry[j]) //equal value and same type operator
- {
- deletesortAry[i]; //delete element and place 'undefined'
- }
- }
- }
- console.log(sortAry); // [undefined × 1, 2, undefined × 1, 4, 5, 7]
- //Now we can remove undefined via using filter function
- varfinalAry = sortAry.filter(function(v)
- {
- return v != undefined
- })
- console.log(finalAry); // [2, 4, 5, 7]
- varary = [2,4,2,5,4,7];
- varsortAry = ary.sort();
- console.log(_.uniq(sortAry)); // [2, 4, 5, 7]
- Outcome: reduced lot much code by using _.uniq function of Loadash.js
- var users = [
- {
- 'user': 'Peter',
- 'age': 36,
- 'isMember': true
- },
- {
- 'user': 'Chris',
- 'age': 40,
- 'isMember': false
- },
- {
- 'user': 'Alan',
- 'age': 20,
- 'isMember': true
- }];
- for (vari in users)
- {
- if (users[i].age > 38)
- {
- var user = users[i];
- break;
- }
- }
- console.log(user); // Object {user: "Chris", age: 40, active: false}
- var users = [
- {
- 'user': 'Peter',
- 'age': 36,
- 'isMember': true
- },
- {
- 'user': 'Chris',
- 'age': 40,
- 'isMember': false
- },
- {
- 'user': 'Alan',
- 'age': 20,
- 'isMember': true
- }];
- _.find(users, function(o)
- {
- return o.age > 38;
- }); //// Object {user: "Chris", age: 40, active: false}
- Outcome: reduced lot much code by using _.find
- function of Loadash.js
- for(vari=0;i<users.length;i++){
- console.log(users[i]);
- }
- _.times(users.length, function(index){
- console.log(users[index]);
- });
- Outcome:readability is better with loadash.js
- varstr="hello world"; // below will truncate string accordingly
- _.truncate(str, {'length':7}); // hell…
- var fruits =['apple','orange','banana','pomegranate'];
- _.shuffle(fruits); // ["orange", "apple", "pomegranate", "banana"]
- var fruits =['apple','orange','banana','pomegranate'];
- _.sample(fruits); // pomegranate; this output could vary
- _.map(users, 'user'); // ["Peter", "Chris", "Alan"]
- //To change all users name into UpperCase
- _.map(users, function(u){varobj=u.user; return obj.toUpperCase();});
- //["PETER", "CHRIS", "ALAN"]
- _.reduce([10, 15, 20, 35, 30, 45], function (accumulator, value) {
- if (value > 20)
- return accumulator + value;
- else
- return 0;
- }); // sum is 110
- var name='Andriana';
- var interest='Programming';
- varstr="Hello, "+name+". Your interest is "+interest;
- console.log(str); // Hello, Andriana. Your interest is Programming
- vartpl = _.template('Hello, <%= name %>. Your interest is <%= interest%>');
- tpl({
- name: 'Andriana', interest: 'Programming'});
- console.log(tpl);//Hello, Andriana. Your interest is Programming
Jonathan SifuentesPosted Dec 8, 2017, 3:31 AM
Decent article, but you're missing spaces here and there, and template strings were already a thing introduced in ES6 by the time you wrote this. True, they aren't 100% cross-compatible, but you definitely should at least make mention of them, especially if someone is learning the language from scratch. Also, other features from ES6 improve the readability even further, such as the `for of` loop: for(var user of users) { // or const, for improved scope reliability console.log(user); }
Kuppurasu NagarajPosted May 22, 2016, 5:32 AM
Nice Sharing..
Manju lata YadavPosted May 19, 2016, 1:27 AM
gr8 efforts to help other developers