{

Frequently used Lodash collection methods with examples


javascript Lodash library example

What is Lodash library?~~~~

Lodash is a javascript library with a lot of useful functions. we can easily integrate with any javascript application.

The collection is of different types like Strings, Arrays, and objects. Javascript provides first-class functions.

First-class functions are like variables, Where we can store a variable, passing as a parameter to a function, and return it from the function.

Functions in javascript are treated as a variable With this, each item in collections arrays and objects can be applied to functions. So each function in the collection object will be applied to each element of collection data.

Let us see various examples of Collection/array objects with examples
It is a continuous post following previous articles on the lodash library.

Lodash find functions examples

There are three functions filter, find, and findRight used to filter or find the element in a collection or array of elements.

filter function example

to iterate the list of arrays/object and filters based on some condition.

var emps = [  
  { 'name': 'franc',  'id': 4, 'salary': 50000 },  
  { 'name': 'kiran',    'id': 20, 'salary': 40000 },  
  { 'name': 'ram', 'id': 31,  'salary': 60000 }  
  { 'name': 'abv', 'id': 21,  'salary': 30000 }  
  
];  
let result=_.filter(emps, function(emp) {  
    return emp.salary >= 50000 ;  
});  

output:

{ 'name': 'franc',  'id': 4, 'salary': 50000 },  
  { 'name': 'ram', 'id': 31,  'salary': 60000 }  
  

The list of employees is filtered based on a condition whose salary is greater than or equal to 50000

lodash find method example

find method iterates the elements and return the first element which matched the condition

var emps = [  
  { 'name': 'franc',  'id': 4, 'salary': 50000 },  
  { 'name': 'kiran',    'id': 20, 'salary': 40000 },  
  { 'name': 'ram', 'id': 31,  'salary': 60000 },  
  { 'name': 'abv', 'id': 21,  'salary': 30000 }  
  
];  
let result=_.find(emps, function(emp) {  
    return emp.salary >= 1000000 ; // outputs  undefined  
});  
let result=_.find(emps, function(e) {  
    return e.salary >= 400000 ; // outputs   { 'name': 'franc',  'id': 4, 'salary': 50000 },  
  
});  

findLast method example lodash

This method also iterates the elements in a collection like a find method but the iteration is from right to left or last to the first element.

// this outputs abv ram kiran franc  
let result=_.findLast(emps, function(emp) {  
console.log(emp.name)   
});  
  
// this outputs franc kiran ram abv  
let result1=_.find(emps, function(emp) {  
console.log(emp.name)  
});  

Iteration Collection/Arrays lodash Example

Iteration is to iterate each element in the collection or arrays of objects. This also uses first-class functions where each element in the iteration is an applied function. It acts as a callback that applies to each element We will see the below examples.

forEach function lodash Example

forEach is to iterate the elements in collections like an array of objects/elements.

Syntax:

forEach(collection/array objects, function(object,key/index,collection) 

object is an each object of collection/array. This required key is an index of a collection/array that starts from zero, the optional collection is an original collection, optional Function will be applied to each element. Returning type is a collection.

_.forEach(['for', 'each'], function(value) {  
  console.log(value);  
});  
//this outputs 'for' and 'each'  
  
_.forEach(emps, function(obj,key,e) {  
    console.log(obj.name+'='+obj.id);  
  
});  
//this outputs   
//franc=4  
//kiran=20  
//ram=31  
//abv=21  

forEachRight lodash method

It is also the same as forEach, but traverses elements from Right to left Parameters and rules are the same as the forEach method

  
_.forEachRight(['a', 'b','c'], function(value) {  
  console.log(value);  
});  
//this outputs 'c','b' and 'a'  
  
_.forEachRight(emps, function(obj,key,e) {  
    console.log(obj.name+'='+obj.id);  
  
});  
//this outputs   
//abv=21  
//ram=31  
//kiran=20  
//franc=4  

Map lodash method example

map method also iterates each element in a collection/array and applies the function to each element and returns a new array.

This method has guarded methods that you can use with different methods like curry, every, and filter methods.

function addTen(n) {  
  return n+10;  
}  
   
console.log(_.map([4, 6], addTen));// outputs  [14, 16]  
console.log(_.map(emps, 'name')); // outputs ["franc", "kiran", "ram", "abv"]  
console.log(_.map(emps, 'salary')); // outputs [50000, 40000, 60000, 30000]  
console.log(_.map(emps, 'id')); // outputs  [4, 20, 31, 21]  

(_.map(emps, ‘id’) is a property shorthand iteration example in which we can use the name of the property directly and the code can be reduced.

Difference between map and forEach in javascript

  • the map is fastest than the foreach method in terms of performances Map returns a new array, source array will not be changed. forEach returns the same array

every() and some() method lodash examples

every method - checks every element of a collection matches with expression

every(collection/array, expression)

some method is reverse of every method, checks any element match with expression.

some(collection/array, expression)  

Syntax:

console.log(_.some(['test', 1, false,'no'], Boolean)) // outputs true  
console.log(_.every(['test', 1, false,'no'], Boolean)) // outputs false  
console.log(_.some(emps, { 'name': 'franc',  'id': 4, 'salary': 50000 })) // outputs true  
console.log(_.every(emps, { 'name': 'franc',  'id': 4, 'salary': 50000 })) // outputs false  

How do convert the object to an array in javascript/lodash?

An object contains the below data, We need to convert it into an array. the map function takes an object and iterates the elements and returns an array.

var objectVariable = {  
    k1: {id: 1, name: 'kiran'},  
    k2: {id: 2, name: 'frank'},  
    k3: {id: 3, name: 'john'}  
};  
  
var arrayVariable = _.map(objectVariable, v=>v);  
console.log('Array 1: ', arrayVariable);  
// returns [{id: 1, name: "kiran"},{id: 2, name: "frank"},{id: 3, name: "john"}]  

THE BEST NEWSLETTER ANYWHERE
Join 6,000 subscribers and get a daily digest of full stack tutorials delivered to your inbox directly.No spam ever. Unsubscribe any time.

Similar Posts
Subscribe
You'll get a notification every time a post gets published here.