javascript jQuery 中是否有类似 array_unique() 的函数?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/25530168/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-28 04:38:22  来源:igfitidea点击:

Is there a function like array_unique() in jQuery?

javascriptphpjqueryarrays

提问by Ozan Kurt

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

我有一个选择,我可以在其中获得一些像数组一样的值 ["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

在每次更改时,我运行以下代码以获取连接到这些值的数组结果,我从 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:

这是我的控制台日志,可让您在选择 2 个选项和结果数组后查看类型数组:

["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.

如您所见,有两次“4689 领导奖”,我不希望这种情况发生。在 PHP 中,我使用该array_unique()函数来消除这些重复值,但我不知道如何在 jQuery 中执行此操作。

回答by Nagy Nick

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);