All VideosJavaScript Tutorials

How to find Unique Values from Array | JavaScript Tutorials in Hindi | Interview Question #32

In JavaScript, you can find unique values from an array using several methods. Here are three common ways:

Method 1: Using the filter() method and indexOf() We use the filter() method to create a new array containing only the elements that pass the test implemented by the provided function. The test checks if the current value’s index is the same as the first index of the value in the array. If it is, the value is unique and passed into the new array.

Method 2: Using the reduce() method We use the reduce() method to create a new array containing only the unique values. The accumulator array starts as an empty array. For each value in the original array, we check if the accumulator array already includes the value. If it doesn’t, we push the value into the accumulator array. Finally, we return the accumulator array.

Method 3: Using the Set object The Set object is a collection of unique values. By spreading the Set object into an array, we can get an array of unique values.

Leave a Reply

Your email address will not be published. Required fields are marked *

error: Content is protected !!