jQuery:如何比较每个索引中具有多个值的两个数组并删除重复项

时间:2022-09-14 12:06:54

I really don't know how to do this. I want to remove the duplicate value of an array that has a multiple value. I tried to use the code from this post Compare arrays with jQuery [duplicate] but i didn't get the result i want. Please help me with this.

我真的不知道该怎么做。我想删除具有多个值的数组的重复值。我试图使用这篇文章中的代码比较数组与jQuery [重复]但我没有得到我想要的结果。请帮我解决一下这个。

var arr1 = ["1,2,3", "4,5", "6", "7,8", "9,10"];
var arr2 = ["2", "4", "7,8"];
var result = []; //expected output: Array["1,3","5","6","9,10"]

$.each(arr1, function(i, val) {
  if ($.inArray(val, arr2) < 0)
    result.push(val);
});
console.log(result);
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

2 个解决方案

#1


If performance is not a huge concern, this will do what you need:

如果性能不是一个大问题,这将满足您的需求:

var arr1   = ["1,2,3", "4,5", "6", "7,8", "9,10"]
var arr2   = ["2", "4", "7,8"].join(',').split(',');
var result = [];

$.each(arr1, function(i, val) {
  var values   = val.split(',');
  var filtered = [];
  $.each(values, function (i, value) {
    if ($.inArray(value, arr2) === -1) {
      filtered.push(value);
    }
  });

  if (filtered.length) result.push(filtered.join(','));
});

#2


First things first, you're going to have to split each of the number strings up and add them to an array as integers.

首先,你必须将每个数字字符串分开并将它们作为整数添加到数组中。

After that, you'll probably want to use something like a hash map to efficiently get the intersect of the arrays. This example should do the job.

之后,您可能希望使用类似哈希映射的内容来有效地获取数组的交叉。这个例子可以完成这项工作。

Here's a simple example. Of course, you would also need to split arr2 + add its values to intersectMap

这是一个简单的例子。当然,您还需要拆分arr2 +将其值添加到intersectMap

intersectMap = {}
for (i = 0; i < arr.length; i++) {
    var values = arr[1].split(",")
    // add these values to a hash map
    for (j = 0; j < values.length; j++) {
        intersectMap[values[j]] = values[j]
    }
}

You will basically want to run through each of the arrays you just created (with the properly split strings), add them to a hash map (intersectMap), and upon completion, add all keys to a new array. This will guarantee that you end up with an array of unique values in the most efficient time possible.

您基本上想要遍历刚刚创建的每个数组(使用正确拆分的字符串),将它们添加到哈希映射(intersectMap),并在完成后将所有键添加到新数组。这将保证您在最有效的时间内获得一系列唯一值。

#1


If performance is not a huge concern, this will do what you need:

如果性能不是一个大问题,这将满足您的需求:

var arr1   = ["1,2,3", "4,5", "6", "7,8", "9,10"]
var arr2   = ["2", "4", "7,8"].join(',').split(',');
var result = [];

$.each(arr1, function(i, val) {
  var values   = val.split(',');
  var filtered = [];
  $.each(values, function (i, value) {
    if ($.inArray(value, arr2) === -1) {
      filtered.push(value);
    }
  });

  if (filtered.length) result.push(filtered.join(','));
});

#2


First things first, you're going to have to split each of the number strings up and add them to an array as integers.

首先,你必须将每个数字字符串分开并将它们作为整数添加到数组中。

After that, you'll probably want to use something like a hash map to efficiently get the intersect of the arrays. This example should do the job.

之后,您可能希望使用类似哈希映射的内容来有效地获取数组的交叉。这个例子可以完成这项工作。

Here's a simple example. Of course, you would also need to split arr2 + add its values to intersectMap

这是一个简单的例子。当然,您还需要拆分arr2 +将其值添加到intersectMap

intersectMap = {}
for (i = 0; i < arr.length; i++) {
    var values = arr[1].split(",")
    // add these values to a hash map
    for (j = 0; j < values.length; j++) {
        intersectMap[values[j]] = values[j]
    }
}

You will basically want to run through each of the arrays you just created (with the properly split strings), add them to a hash map (intersectMap), and upon completion, add all keys to a new array. This will guarantee that you end up with an array of unique values in the most efficient time possible.

您基本上想要遍历刚刚创建的每个数组(使用正确拆分的字符串),将它们添加到哈希映射(intersectMap),并在完成后将所有键添加到新数组。这将保证您在最有效的时间内获得一系列唯一值。