Categories
JavaScript

Useful Lodash Methods — Finding Uniques

Spread the love

Lodash is a utility library that has lots of methods for manipulating objects. It has stuff that we use all the time and also things that we don’t use frequently or don’t think of using.

In this article, we’ll look at how to use Lodash methods to create data structures with unique entries.

_.union([arrays])

The union method takes a comma-separated list of arrays and return an array with the unique entries from all the arrays combined. The results are chosen from the first array in which the value occurs.

For instance, we can use it as follows:

import * as _ from "lodash";

const arr = _.union([1, 2, 3], [2, 3, 4]);
console.log(arr);

Then arr is [1, 2, 3, 4] since we have 1, 2, 3, and 4 included as entries of one or both arrays.

.unionBy([arrays], [iteratee=.identity])

The unionBy method takes a list of arrays as the first arguments, and then a function that’s called as the values are being iterated through. The function determines what values are considered unique. The results are chosen from the first array in which the value occurs.

It returns a new array of combined values.

For instance, we can use it as follows:

import * as _ from "lodash";

const arr = _.unionBy([1, 2.1, 2.7, 3], [2.8, 3.1, 4], Math.round);
console.log(arr);

Then we get:

[1, 2.1, 2.7, 4]

as the value of arr since we called Math.round on each and then compare them to see which ones are the same after rounding with Math.round .

There’s also a shorthand to find the unique value by the property of the object. For example, we can write:

import * as _ from "lodash";

const arr = _.unionBy([{ x: 2 }], [{ x: 2 }, { x: 1 }], "x");
console.log(arr);

Then we get:

[
  {
    "x": 2
  },
  {
    "x": 1
  }
]

as the value of arr .

_.unionWith([arrays], [comparator])

We can use unionWith to takes one more array and a comparator function to compare the values each item and see which ones are equal. It takes the first one from the list of items that are considered the same.

Then it put those values in the array and returns it. For example, we can use it as follows:

import * as _ from "lodash";

const arr1 = [{ x: 1, y: 3 }, { x: 2, y: 1 }];
const arr2 = [{ x: 1, y: 1 }, { x: 1, y: 3 }];
const arr = _.unionWith(arr1, arr2, _.isEqual);

Then we write have:

[
  {
    "x": 1,
    "y": 3
  },
  {
    "x": 2,
    "y": 1
  },
  {
    "x": 1,
    "y": 1
  }
]

as the value of arr as it takes the first value that’s unique in the arrays and put them in the returned array.

Photo by Daniel McCarthy @themccarthy on Unsplash

_.uniq(array)

The uniq method takes an array and then returns a new array that has the duplicates eliminated.

For instance, we can use it as follows:

import * as _ from "lodash";

const arr = _.uniq([1, 2, 3, 4, 4, 5]);

Then we get:

[
  1,
  2,
  3,
  4,
  5
]

as the value of arr .

Conclusion

Lodash has methods to return arrays by getting unique values from one or more arrays and return them in a new array. Some also support different ways of determining if the value is unique by our criteria like the unionBy method.

By John Au-Yeung

Web developer specializing in React, Vue, and front end development.

Leave a Reply

Your email address will not be published. Required fields are marked *