Is there a function like array_unique () in jQuery?

I have a choice where I can get some values ​​as an array like ["1","2","3"]

On every change, I run the following code to get the result of the array that is associated with these values ​​that 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 so you can see the type array after I have 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 can see, there are two times "4689 Leadership Award" and I don't want that to happen. In PHP I am using a function array_unique()

to eliminate these duplicate values, but I don't know how to do it in jQuery.

+3


source to share


1 answer


try this:



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

      

+8


source







All Articles