How to Get Distinct Values from Array in Javascript
In this tutorial, you will learn how to get distinct values from array in javascript. Arrays are a popular way to represent a collection of related elements. It’s possible that the items have different data types. From a developer’s point of view, it can be a bit tricky to get distinct values from array.
This tutorial focuses primarily on the string type. The functions toLowerCase()
and toUpperCase()
can only be used with strings and cannot be used with numbers. We must rely on other built-in methods and loops to achieve our aim because there is no built-in method to delete duplicate elements from an array.
In the following example, we have a global array of fruits. Initially, we will display all fruit names on the screen. Upon click of a button, we will get distinct values from array and update our list. Please have a look over the code example and the steps given below.
HTML & CSS
- We have 2 elements in the HTML file (
button
andul
). - The
innerText
for thebutton
element is“Remove Duplicates”
. - The
ul
element is empty as of now and we will populate it dynamically using javascript. - We have also included our javascript file
script.js
with ascript
tag at the bottom.
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <meta http-equiv="X-UA-Compatible" content="ie=edge"> <title>Document</title> </head> <body> <button>Remove Duplicates</button> <ul></ul> <script src="script.js"></script> </body> </html>
Javascript
- We have a global variable
fruits
and it holds an array of strings. - We have the
addFruits()
method which will populate ourul
element with list of fruit names. - In
addFruits()
method, we are simply looping through thefruits
array and creating a template string with a bunch ofli
elements. - We are displaying that list in the
ul
element by using theinnerHTML
property. - We are calling the
addFruits()
method to display our initial fruits list. - We have selected the
button
element using thedocument.querySelector()
method and attached theclick
event listener to it.
Case Sensitive Comparison
- We are using the
Set
constructor to create aSet
of fruits. By default, you cannot have duplicate items in aSet
. We are storing our fruitsSet
in themySet
variable - We are using
Array.from()
method to convertSet
into an array. We are assigning it back to thefruits
variable. - We are calling the
addFruits()
method to update our fruits list.
Case Insensitive Comparison
- We are using the
map()
method to loop through all items in the array. Inside the loop, we are using thetoLowerCase()
method to have all fruit names in the lowercase. - We are using the
filter()
method to remove duplicate items from the array. Inside the loop, we are simply using theindexOf()
method to check if a certain item is already present or not. - We are again using the
map()
method to capitalize the first letter of each fruit name. - Inside the loop, we are using the index of 0 to get the first letter and then using the
toUpperCase()
method to capitalize it. We are using theslice()
method to slice the string from index 1. We are joining the capitalized letter and sliced string by using the addition (+
) operator. - We are calling the
addFruits()
method to update our fruits list.
let fruits = ['Plum', 'Apple', 'Orange', 'Watermelon', 'Plum', 'Orange', 'Apple']; function addFruits(){ let template = fruits.map(fruit => `<li>${fruit}</li>`).join('\n'); document.querySelector('ul').innerHTML = template; } addFruits(); document.querySelector('button').addEventListener('click', () =>{ /* let mySet = new Set(fruits); fruits = Array.from(mySet); addFruits(); */ fruits = fruits.map(fruit => fruit.toLowerCase()); fruits = fruits.filter((item, index) => fruits.indexOf(item) == index); fruits = fruits.map(fruit => fruit[0].toUpperCase() + fruit.slice(1)); addFruits(); });