如何使用jquery在数组中查找重复项 [英] How to find the duplicates in an array using jquery
问题描述
我有一个jQuery数组:
I have a jQuery array:
var arr = $('input[name$="recordset"]');
我得到的数组值如8或6
I am getting the value of array like 8 or 6
如果数组值重复或重复,我需要显示请不要重复值。如果不是,我需要继续。
If array values are repeating or duplicate I need to show "please do not repeat the values". If not I need to proceed further.
使用jQuery可以告诉我如何找到重复的值吗?
Using jQuery can anybody tell me how to find the duplicate values?
推荐答案
var unique_values = {};
var list_of_values = [];
$('input[name$="recordset"]').
each(function(item) {
if ( ! unique_values[item.value] ) {
unique_values[item.value] = true;
list_of_values.push(item.value);
} else {
// We have duplicate values!
}
});
我们正在做的是创建一个哈希来列出我们已经看过的值,以及一个列表存储所有唯一值。对于每个输入选择器返回我们正在检查我们是否已经看到了该值,如果不是,我们将它添加到我们的列表中和将它添加到已经看到的哈希值中价值观。
What we're doing is creating a hash to list values we've already seen, and a list to store all of the unique values. For every input the selector returns we're checking to see if we've already seen the value, and if not we're adding it to our list and adding it to our hash of already-seen-values.
这篇关于如何使用jquery在数组中查找重复项的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!