This ESLint plugin removes duplicate elements in arrays by comparing their source code text. Arrays must be specially marked with a // @remove-duplicates comment.
You'll first need to install ESLint:
npm i eslint --save-dev
# or
yarn add eslint --devNext, install eslint-plugin-remove-duplicates:
npm install eslint-plugin-remove-duplicates --save-dev
# or
yarn add eslint-plugin-remove-duplicates --devAdd remove-duplicates to the plugins section of your .eslintrc.js configuration file. You can omit the eslint-plugin- prefix:
module.exports = {
plugins: [
'remove-duplicates'
]
};Then, in any JavaScript file, you can use the // @remove-duplicates comment to mark an array for duplicate removal:
// @remove-duplicates
const myArray = ['a', 'b', 'a', 'c', 'b', 'a'];
// After running ESLint with this plugin, myArray will be:
// const myArray = ['a', 'b', 'c'];Here's an example of how the plugin works:
Before:
// @remove-duplicates
const myList = ["apple", "banana", "apple", "orange", "banana", "grape"];After running ESLint:
// @remove-duplicates
const myList = ["apple", "banana", "orange", "grape"];