<tutorialjinni.com/>

Get Unique Values in an Array - JavaScript

Posted Under: JavaScript, Tutorials on Dec 22, 2016
Get Unique Values in an Array - JavaScript
This tutorial will demonstrate how to find unique values in any given array and also how the elements return are in the same position as they were in the original subject array, i.e. only duplicates are removed, this is very important that the elements remain in their order but not shuffled.

Before ECMAScript version 5 two loops are required to do this. But with ECMAScript 6 it become a one liner operation.
<script>
    subjectArray=new Array();
    subjectArray[0]=1;
    subjectArray[1]=2;
    subjectArray[2]=1;
    subjectArray[3]=3;
    subjectArray[4]=1;
    subjectArray[5]="one";
    subjectArray[6]="two";
    subjectArray[7]="one";

    uniqueArray=Array.from(new Set(subjectArray));

    alert(uniqueArray);

    // Outputs [1,2,3,one,two]
</script>


imgae