JavaScript

JavaScript – Find unique array

Here I will explain how to find an unique array from an array which has duplicates.

There are different methods to find the unique elements in an array. Here lets use ES6 functions reduce and find.

function uniqueArray(array) {
  var newUniqueArray = array.reduce((accumulator, currentValue) => {
    if (!accumulator.find(arrayItem => arrayItem === currentValue)) {
      accumulator.push(currentValue);
    }
    return accumulator;
  }, []);

  return newUniqueArray;
}

Checkout the working code sample below

See the Pen Unique array using reduce and find by Kiran (@kiranvj) on CodePen.

Happy coding…..

Computer engineer interested in programming, electronics, application architecture, data visualization, automation and performance optimization.

Leave a Reply