Lodash is my favorite JavaScript library that includes many useful data manipulation utilities in functional programming style. It can run in Node.js and browsers.

The below snippet is an example of using 2 functions filter and includes to find duplications in an array.

const _ = require('lodash')

const values = [1,2,2,3,4,1]
const duplications = _.filter(values, (value, i) => {
 return _.includes(values, value, i + 1)
})

console.log(duplications) 
// => [1, 2]

Explain:

  • filter takes the array or collection we want to filter/select values that match the supplied predicate function where (value, i) is each element in the array and its index
    • includes checks whether the value is in the array from next index (i + 1). If it returns true, that means the same value is found again, hence a duplication; therefore, it will be included in the filtered result.
Share This