DEV Community

Cover image for Useful Lodash Methods
minoblue
minoblue

Posted on

Useful Lodash Methods

Shuffle the elements of an Array

shuffle() method in Lodash to randomize the elements in an array.

let people = ["Jane", "Adam", "Nitish", "Joe", "Rakesh", "Will", "Andrew", "Samantha"];
console.log(_.shuffle(people));
// outputs: ['Joe', 'Samantha', 'Adam', 'Jane', 'Andrew', 'Rakesh', 'Will', 'Nitish']
Enter fullscreen mode Exit fullscreen mode

Remove falsey values from an Array

compact() method to filter out all the falsey values, including false, null, undefined, NaN, the empty string "", and 0. Here is an example:

let values = ["Chilly", "Sunny", undefined, '', "Sunny", false, "Cloudy"];
console.log(_.compact(values));
// Outputs: ['Chilly', 'Sunny', 'Sunny', 'Cloudy']
Enter fullscreen mode Exit fullscreen mode

Difference between two Arrays

The difference() method from Lodash will return a new array that contains all the values from the first array that are not part of the second array.

let people = ["Adam", "Nitish", "Joe", "Rakesh", "Will", "Andrew", "Samantha"];
let team_a = ["Adam", "Andrew", "Jane", "Samantha"];
let team_b = _.difference(people, team_a);
console.log(team_b);
// Outputs: ['Nitish', 'Joe', 'Rakesh', 'Will']
Enter fullscreen mode Exit fullscreen mode

Compose object from key-value pairs

The inverse of _.toPairs() this method returns an object composed from key-value pairs.

_.fromPairs([['a', 1], ['b', 2]]);
// => { 'a': 1, 'b': 2 }
Enter fullscreen mode Exit fullscreen mode

Grouping Array elements together

The _.groupBy() can use to group different elements of any array.

_.groupBy([6.1, 4.2, 6.3], Math.floor);
// => { '4': [4.2], '6': [6.1, 6.3] }
Enter fullscreen mode Exit fullscreen mode

Deep comparison between two values

Performs a deep comparison between two values to determine if they are equivalent.

var object = { 'a': 1 };
var other = { 'a': 1 };

_.isEqual(object, other);
// => true
Enter fullscreen mode Exit fullscreen mode

Speedy emails, satisfied customers

Postmark Image

Are delayed transactional emails costing you user satisfaction? Postmark delivers your emails almost instantly, keeping your customers happy and connected.

Sign up

Top comments (0)

The Most Contextual AI Development Assistant

Pieces.app image

Our centralized storage agent works on-device, unifying various developer tools to proactively capture and enrich useful materials, streamline collaboration, and solve complex problems through a contextual understanding of your unique workflow.

👥 Ideal for solo developers, teams, and cross-company projects

Learn more

👋 Kindness is contagious

Explore a sea of insights with this enlightening post, highly esteemed within the nurturing DEV Community. Coders of all stripes are invited to participate and contribute to our shared knowledge.

Expressing gratitude with a simple "thank you" can make a big impact. Leave your thanks in the comments!

On DEV, exchanging ideas smooths our way and strengthens our community bonds. Found this useful? A quick note of thanks to the author can mean a lot.

Okay