9

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.

Kai Mattern
  • 3,090
  • 2
  • 34
  • 37
Ozan Kurt
  • 3,731
  • 4
  • 18
  • 32
  • jQuery doesn't have a method that will do this. However, it is certainly possible to do this with a simple for loop or one of several array methods. – Kevin B Aug 27 '14 at 14:43
  • 2
    I have a question, `type` is a value, so it's clearly a string, yet you're using it as an array in `$.inArray`, how is that working ? – adeneo Aug 27 '14 at 14:48
  • It works because strings also have an indexOf method. The code doesn't seem to make sense, but it will definitely do "something" based on the result of indexOf, which will be different for each type chosen. It almost seems as if the inArray ins't needed at all, and should be able to just compare string value to string. – Kevin B Aug 27 '14 at 14:53
  • @KevinB - I tested it, so it know it actually works as jQuery calls `indexOf`, I was just wondering if the OP knew why he was using `$.inArray` on a string, which is clearly not what it's intended for. – adeneo Aug 27 '14 at 14:57

1 Answers1

22

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);
Nagy Nick
  • 745
  • 9
  • 19
  • 1
    Nice answer.I loved it.I try to find more but my satisfaction completed with your answer. – Ashish4434 Mar 29 '18 at 06:14
  • 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; ` – Renan Coelho Mar 21 '19 at 23:31
  • 1
    @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))` – Moisés Márquez May 07 '19 at 16:03
  • Yes, it can be, but I prefer code to be as readable as possible, rather then short. https://stackoverflow.com/questions/952194/should-code-be-short-concise – Nagy Nick May 08 '19 at 10:59
  • Notice, that this method doesn't change passed array, so you should call it like `array = unique(array);`. – Boolean_Type Sep 11 '19 at 11:10