Categories
JavaScript Answers

How to get duplicate values from an array with JavaScript Lodash?

Spread the love

To get duplicate values from an array with JavaScript Lodash, we can use the filter and includes methods.

For instance, we write

const dups = _.filter(arr, (val, i, iteratee) =>
  _.includes(iteratee, val, i + 1)
);

to call filter with arr and a callback that calls includes to check if val is included in the arr array itself from index i + 1 and oin.

iteratee is the arr array.

An array with the duplicates is returned.

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 *