使用jQuery数组查找重复项使用唯一的方法。

huangapple go评论45阅读模式
英文:

Find duplicates in jquery array using unique

问题

我有以下的JS数组。

$arrTest1 = ['46819', '46819', '46819'];
$arrTest2 = ['46819', '46802', '46819'];

let len1 = arrTest1.length;
let len2 = $.unique(arrTest1).length; 

if (len1 > len2) {
  alert('Found duplicate');
} else {
  alert('Did not find duplicate');
}

let len3 = arrTest2.length;
let len4 = $.unique(arrTest2).length; 

if (len3 > len4) {
  alert('Found duplicate');
} else {
  alert('Did not find duplicate');
}

对于第一个数组 $arrTest1,它正常工作并显示找到了重复项,但对于 $arrTest2,它显示为未找到重复项。我漏掉了什么吗?

$.unique 对于 $arrTest2 不起作用,而对于 $arrTest1 它可以正常工作。

英文:

I have the following JS arrays.

$arrTest1 = ['46819', '46819', '46819'];
$arrTest2 = ['46819', '46802', '46819'];

let len1 = arrTest1.length;
let len2 = $.unique(arrTest1).length; 

if (len1 > len2) {
  alert('Found duplicate');
} else {
  alert('Did not find duplicate');
}


let len3 = arrTest2.length;
let len4 = $.unique(arrTest2).length; 

if (len3 > len4) {
  alert('Found duplicate');
} else {
  alert('Did not find duplicate');
}

For first array $arrTest1 its working and showing found duplicate but for $arrTest2 its showing as Did not find duplicate. Did I missed something?

$.unique is not working for $arrTest2 whereas for $arrTest1 it works fine.

答案1

得分: 0

文档:

描述:对DOM元素数组进行就地排序,同时删除重复项。请注意,这仅适用于DOM元素的数组,而不适用于字符串或数字

$arrTest1 = ['46819', '46819', '46819'];
$arrTest2 = ['46819', '46802', '46819'];

let len1 = $arrTest1.length;
let len2 = [...new Set($arrTest1)].length;

if (len1 > len2) {
  alert('发现重复项');
} else {
  alert('未发现重复项');
}

let len3 = $arrTest2.length;
let len4 = [...new Set($arrTest2)].length;

if (len3 > len4) {
  alert('发现重复项');
} else {
  alert('未发现重复项');
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>

您可以像我的示例一样使用Set

英文:

Documentation:

> Description: Sorts an array of DOM elements, in place, with the
> duplicates removed. Note that this only works on arrays of DOM
> elements, not strings or numbers.

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-js -->

$arrTest1 = [&#39;46819&#39;, &#39;46819&#39;, &#39;46819&#39;];
$arrTest2 = [&#39;46819&#39;, &#39;46802&#39;, &#39;46819&#39;];

let len1 = $arrTest1.length;
let len2 = [...new Set($arrTest1)].length; 

if (len1 &gt; len2) {
  alert(&#39;Found duplicate&#39;);
} else {
  alert(&#39;Did not find duplicate&#39;);
}


let len3 = $arrTest2.length;
let len4 = [...new Set($arrTest2)].length; 

if (len3 &gt; len4) {
  alert(&#39;Found duplicate&#39;);
} else {
  alert(&#39;Did not find duplicate&#39;);
}

<!-- language: lang-html -->

&lt;script src=&quot;https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js&quot;&gt;&lt;/script&gt;

<!-- end snippet -->

You can use Set like my example

答案2

得分: 0

你可以使用以下代码:

  1. $.merge() 用于将 array1array2 合并成 combinedArray
  2. $.grep() 用于过滤 combinedArray 并找到重复的值,这些值存储在 duplicateValues 中。

请参考下面的代码

$arrTest1 = ['46819', '46819', '46819'];
$arrTest2 = ['46819', '46802', '46819'];

const combinedArray = $.merge($.merge([], $arrTest1), $arrTest2);

// 查找重复值
const duplicateValues = $.grep(combinedArray, function (element, index) {
  return index !== $.inArray(element, combinedArray);
});

if (duplicateValues.length) {
  alert('找到重复项');
} else {
  alert('未找到重复项');
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
英文:

You can use

  1. $.merge() is used to combine array1 and array2 into combinedArray.
  2. $.grep() is used to filter combinedArray and find the duplicate values, which are stored in duplicateValues.

Refer below Code

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-js -->

$arrTest1 = [&#39;46819&#39;, &#39;46819&#39;, &#39;46819&#39;];
$arrTest2 = [&#39;46819&#39;, &#39;46802&#39;, &#39;46819&#39;];

const combinedArray = $.merge($.merge([], $arrTest1), $arrTest2);

// Find duplicate values
const duplicateValues = $.grep(combinedArray, function (element, index) {
  return index !== $.inArray(element, combinedArray);
});

if(duplicateValues.length) {
  alert(&#39;Found duplicate&#39;);
} else {
  alert(&#39;Did not find duplicate&#39;);
}

<!-- language: lang-html -->

&lt;script src=&quot;https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js&quot;&gt;&lt;/script&gt;

<!-- end snippet -->

答案3

得分: 0

$.uniqueV.3.0 及以上版本中已被移除,您可以使用 $.grep() 来实现相同的功能。

var arrTest1 = ['46819', '46819', '46819'];
var arrTest2 = ['46819', '46802', '46819'];

var duplicatesTest1 = $.grep(arrTest1, function (e, i) {
    return $.inArray(e, arrTest1) !== i;
});

if (duplicatesTest1.length > 0) {
    alert('在 arrTest1 中找到重复项');
} else {
    alert('在 arrTest1 中未找到重复项');
}

var duplicatesTest2 = $.grep(arrTest2, function (e, i) {
    return $.inArray(e, arrTest2) !== i;
});

if (duplicatesTest2.length > 0) {
    alert('在 arrTest2 中找到重复项');
} else {
    alert('在 arrTest2 中未找到重复项');
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
英文:

$.unique has be removed in V.3.0 and above you can do it using $.grep()

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-js -->

var arrTest1 = [&#39;46819&#39;, &#39;46819&#39;, &#39;46819&#39;];
var arrTest2 = [&#39;46819&#39;, &#39;46802&#39;, &#39;46819&#39;];

var duplicatesTest1 = $.grep(arrTest1, function (e, i) {
    return $.inArray(e, arrTest1) !== i;
});

if (duplicatesTest1.length &gt; 0) {
    alert(&#39;Found duplicate in arrTest1&#39;);
} else {
    alert(&#39;Did not find duplicate in arrTest1&#39;);
}

var duplicatesTest2 = $.grep(arrTest2, function (e, i) {
    return $.inArray(e, arrTest2) !== i;
});

if (duplicatesTest2.length &gt; 0) {
    alert(&#39;Found duplicate in arrTest2&#39;);
} else {
    alert(&#39;Did not find duplicate in arrTest2&#39;);
}

<!-- language: lang-html -->

&lt;script src=&quot;https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js&quot;&gt;&lt;/script&gt;

<!-- end snippet -->

huangapple
  • 本文由 发表于 2023年6月5日 17:58:03
  • 转载请务必保留本文链接:https://go.coder-hub.com/76405284.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定