65.9K
CodeProject is changing. Read more.
Home

Get Unique Values from a JavaScript Array

starIconstarIconstarIconstarIconstarIcon

5.00/5 (3 votes)

Aug 5, 2013

CPOL
viewsIcon

64031

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

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;
}