How to Remove Duplicate Values from a JavaScript Array?

Posted by LaravelIndia - 4 months ago

Example : 1 Defining function to get unique values from an array

<script>     
    function getUnique(array){
        var uniqueArray = [];

        for(var value of array){
            if(uniqueArray.indexOf(value) === -1){
                uniqueArray.push(value);
            }
        }
        return uniqueArray;
    }

    var names = ['JS','PHP','Ruby','Node','Python','PHP','JS'];
    var uniqueNames = getUnique(names);
    console.log(uniqueNames);
</script>