- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
How to get all unique values in a JavaScript array?
Following is the code for getting all unique values in a JavaScript array −
Example
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8" /> <meta name="viewport" content="width=device-width, initial-scale=1.0" /> <title>Document</title> <style> body { font-family: "Segoe UI", Tahoma, Geneva, Verdana, sans-serif; } .result, .sample { font-size: 18px; font-weight: 500; color: rebeccapurple; } .result { color: red; } </style> </head> <body> <h1>Get all unique values in a array</h1> <div class="sample"></div> <div class="result"></div> <button class="Btn">Click here</button> <h3>Click on the above button to remove the duplicate values from the above array</h3> <script> let resultEle = document.querySelector(".result"); let sampleEle = document.querySelector(".sample"); let arr = [2, 3, 4, 2, 3, 4, "A", "A", "B", "B"]; sampleEle.innerHTML = "arr = " + arr; document.querySelector(".Btn").addEventListener("click", () => { let set1 = new Set(arr); resultEle.innerHTML = "arr = " + [...set1] + "<br>"; }); </script> </body> </html>
Output
On clicking the ‘Click here’ button −
- Related Articles
- Summing all the unique values of an array - JavaScript
- JavaScript How to get all 'name' values in a JSON array?
- Summing up unique array values in JavaScript
- How to get the numbers which can divide all values in an array - JavaScript
- JavaScript: How to filter out Non-Unique Values from an Array?
- Filter unique array values and sum in JavaScript
- Extract unique values from an array - JavaScript
- How to get the most common values in array: JavaScript ?
- How to get unique values from MongoDB collection?
- Finding the sum of unique array values - JavaScript
- Get unique item from two different array in JavaScript
- Get unique values from a list in Python
- How to make all values in an R data frame unique?
- How to get the first n values of an array in JavaScript?
- Find unique and biggest string values from an array in JavaScript

Advertisements