How to use reduce() Method In Javascript

The reduce() method is used to reduce the elements of the array and combine them into a final array based on some reducer function that you pass.

Example: In this example, we will see the use of the reduce() method.

Javascript
let arr = ["apple", "mango",
          "apple", "orange", "mango", "mango"];
  
function removeDuplicates(arr) {
    let unique = arr.reduce(function (acc, curr) {
        if (!acc.includes(curr))
            acc.push(curr);
        return acc;
    }, []);
    return unique;
}
console.log(removeDuplicates(arr));

Output
[ 'apple', 'mango', 'orange' ]

How to get all unique values (remove duplicates) in a JavaScript array?

We are going to learn How to get all unique values (remove duplicates) in a JavaScript array. Given an array with various values, our objective is to filter out any duplicates and display the resulting array in the console.

get all unique values (remove duplicates) in a JavaScript array

We can get all unique values in a JavaScript array in the following ways:

Table of Content

  • Method 1: Using for loop
  • Method 2: Using the Array filter() method:
  • Method 3: Using set() Method
  • Method 4: Using reduce() Method
  • Method 5: Using forEach() Method
  • Method 6: Using indexOf() Method
  • Method 7: Using Underscore.js _.uniq() Function

Similar Reads

Method 1: Using for loop

for loop method checked each value of the original array (listArray) with each value of the output array (outputArray) where the duplicate values are removed. If the current value does not exist in the output array with unique values, then add the element to the output array....

Method 2: Using the Array filter() method:

The arr.filter() function is used to create a new array from an existing array consisting of only those elements from the given array which satisfy a condition set by the argument function....

Method 3: Using set() Method

A set is a collection of items that are unique i.e. no element can be repeated. Set in ES6 are ordered: elements of the set can be iterated in the insertion order. The set can store any type of value whether primitive or objects....

Method 4: Using reduce() Method

The reduce() method is used to reduce the elements of the array and combine them into a final array based on some reducer function that you pass....

Method 5: Using forEach() Method

By using the forEach() method, we can iterate over the elements in the array, and we will push into the new array if it doesn’t exist in the array....

Method 6: Using indexOf() Method

The indexOf() method is used to find the first index of occurrence of an array element. we can iterate over the elements in the array, and we will push into the new array if it doesn’t exist in the resultant array....

Method 7: Using Underscore.js _.uniq() Function

We can also use a third-party library such as Lodash or Underscore.js to remove duplicate elements from a Javascript array. The _.uniq() function returns the array which does not contain duplicate elements....