Click here to Skip to main content
15,881,852 members
Articles / Programming Languages / Javascript
Tip/Trick

Get Unique Values from a JavaScript Array

Rate me:
Please Sign up or sign in to vote.
5.00/5 (3 votes)
5 Aug 2013CPOL 63.5K   2   3
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)


Written By
Architect
Netherlands Netherlands

Read my personal blog at www.manasbhardwaj.net.


Comments and Discussions

 
Suggestionanother approach Pin
Member 1388426623-Jun-18 7:12
Member 1388426623-Jun-18 7:12 
QuestionDoes this work for arrays of objects or just primitives? Pin
AmoebaMan17-25-Aug-13 12:20
AmoebaMan17-25-Aug-13 12:20 
AnswerRe: Does this work for arrays of objects or just primitives? Pin
Florian Rappl5-Aug-13 12:39
professionalFlorian Rappl5-Aug-13 12:39 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.