Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles / Languages / Javascript

Get Unique Values from a JavaScript Array

5.00/5 (3 votes)
5 Aug 2013CPOL 64K  
I have seen this question so many times on forums, so decided to put it as tip. Here is a simple code snippet to get unique list of values out of javascript array. It makes use of jQuery to do a look up.

Introduction

I have seen this question so many times on forums, so decided to put it as tip. Here is a simple code snippet to get a unique list of values out of a JavaScript array. It makes use of jQuery to do a look up.

Using the code

JavaScript
function GetUnique(inputArray)
{
    var outputArray = [];
    
    for (var i = 0; i < inputArray.length; i++)
    {
        if ((jQuery.inArray(inputArray[i], outputArray)) == -1)
        {
            outputArray.push(inputArray[i]);
        }
    }
   
    return outputArray;
}

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)