To Remove Duplicates From an Array in JavaScript Without Using For Loop, we use two JavaScript built-in method or function,
- Array.from() method
- Set() constructor
Array.from() method helps us to create an array from,
- array like objects (Object which have length or index properties) or
- iterable objects (like map and set)
Set() constructor is used to create set object and it store unique values of any type.
Source code
1 2 3 4 5 6 7 8 |
<script> // define an array const numbers = [4, 1, 4, 20, 13, 8, 1, 1, 'b', 'a', 'b']; // useing Array.from() method const uniqueNumbers = Array.from(new Set(numbers)); // print result document.write(uniqueNumbers); </script> |
Output:
4,1,20,13,8,b,a
Live view of Remove Duplicates From an Array Without Using Loop in JS
See the Pen remove duplicates from array javascript without using For loop by Bikash Panda (@phpcodertech) on CodePen.
Here is complete tutorial how we remove duplicate value from an array without using any loop in JS.
I hope it can help you.
To know more about Set() constructor https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Set/Set.
Also Check:
- Convert JSON to Array PHP
- How JavaScript Filter Function work (Complete Guide With Example)
- How JavaScript For Loop Works (Complete Guide With Example)
- jQuery Contact Form Send Email Using Ajax
Happy Coding..!
4 Replies to “Remove Duplicates From an Array in JavaScript Without Using For Loop”