Click here to Skip to main content
Click here to Skip to main content

Tagged as

Go to top

Get Unique Values from a JavaScript Array

, 5 Aug 2013
Rate this:
Please Sign up or sign in to vote.
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;
}

License

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

Share

About the Author

Manas Bhardwaj
Architect
Netherlands Netherlands
Read my personal blog at www.manasbhardwaj.net.
Follow on   Twitter

Comments and Discussions

 
QuestionDoes this work for arrays of objects or just primitives? PinmemberAmoebaMan17-25-Aug-13 12:20 
AnswerRe: Does this work for arrays of objects or just primitives? PinmvpFlorian Rappl5-Aug-13 12:39 

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

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

| Advertise | Privacy | Mobile
Web04 | 2.8.140916.1 | Last Updated 5 Aug 2013
Article Copyright 2013 by Manas Bhardwaj
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid