Is there a function like array_unique() in jQuery?

13,297

try this:

function unique(array){
  return array.filter(function(el, index, arr) {
      return index == arr.indexOf(el);
  });
}

working example:

const startingArray = [1, 2, 2, 3, 2, 3, 3, 3];

function unique(array){
  return array.filter(function(el, index, arr) {
      return index == arr.indexOf(el);
  });
}

const uniqueArray = unique(startingArray);

console.log(uniqueArray);

Share:
13,297
Ozan Kurt
Author by

Ozan Kurt

Hi there, I am a developer working with the company 4global as a senior web developer. I am currently studying informations technologies in Karlsruhe Institut of Technology.

Updated on June 25, 2022

Comments

  • Ozan Kurt
    Ozan Kurt almost 2 years

    I have a select where i can get some values as array like ["1","2","3"]

    On every change i run the following code to get a result of array which is connected to these values i get from select

    $('#event-courses-type').on('change',function(){
    
        type = $('#event-courses-type').val();
    
        console.log(type);
    
        var var1 = ["4689 Leadership Award", "UKCC Level 1", "UKCC Level 2", "UKCC Level 3", "UKCC Level 4", "Old WHU Award", "None at present"];
        var var2 = ["4689 Leadership Award", "GB-wide Level 1", "Old Level 1 (Pre January 2012)", "Level 2", "Level 3", "EHF", "FIH", "None at present"];
    
        var var5 = ["D32/33", "A1 Assessor", "CTS", "IAPS", "PTLLS", "AVRA", "Umpire Educator Training", "Umpire Assessor Training"];
        var var6 = ["D32/33", "A1 Assessor", "CTS", "IAPS", "PTLLS", "AVRA", "Umpire Educator Training", "Umpire Assessor Training"];
    
        var results = [];
    
        if ($.inArray("1",type) != -1) {
            var results = $.merge(var1, results);
        }
        if ($.inArray("2",type) != -1) {
            var results = $.merge(var2, results);
        }
        if ($.inArray("5",type) != -1) {
            var results = $.merge(var5, results);
        }
        if ($.inArray("6",type) != -1) {
            var results = $.merge(var6, results);
        }
    
        console.log(results);
    )};
    

    Here is my console log to let you see the type array after i selected 2 options and the results array:

    ["1", "2"] ------------------- add_event.php:802
    
    ["4689 Leadership Award", "GB-wide Level 1", "Old Level 1 (Pre January 2012)", "Level 2", "Level 3", "EHF", "FIH", "None at present", "4689 Leadership Award", "UKCC Level 1", "UKCC Level 2", "UKCC Level 3", "UKCC Level 4", "Old WHU Award", "None at present"]
    

    As you see there is two times "4689 Leadership Award" and i dont want this to happen. In PHP i use the array_unique() function to eliminate these duplicated values but i dont know how to do it in jQuery.

  • Ashish4434
    Ashish4434 about 6 years
    Nice answer.I loved it.I try to find more but my satisfaction completed with your answer.
  • Renan Coelho
    Renan Coelho about 5 years
    and a nice trick to check for unique in an array of objects, you may convert it to a vetor BEFORE using this function: const genres = this.state.movies.map(movie => movie.genre); const uniqueGenres = genres.filter((genre, index, arr) => index == arr.indexOf(genre)); return uniqueGenres;
  • Moisés Márquez
    Moisés Márquez almost 5 years
    @RenanCoelho answer is correct but can be shortened with something like this: this.state.movies.map(e=>e.genre).filter((e, i, a) => i === a.indexOf(e))
  • Nagy Nick
    Nagy Nick almost 5 years
    Yes, it can be, but I prefer code to be as readable as possible, rather then short. stackoverflow.com/questions/952194/should-code-be-short-conc‌​ise
  • Boolean_Type
    Boolean_Type over 4 years
    Notice, that this method doesn't change passed array, so you should call it like array = unique(array);.