jquery javascript检查数组中的重复

时间:2014-11-11 10:58:03

标签: javascript jquery

您好我需要更改数组格式并检查数组中的重复键是否相同。如果存在相同的密钥,那么我将获得密钥的值

              var selectedItems = ["Group1", "Group1"] ;

                  if (arrHasDupes(selectedItems)) // this just calls the function to test it
                    {
                        alert('duplicates found');
                        // $(this).prop("checked", false);
                        return false;
                    }
                    else
                    {
                        //alert('no duplicates found');
                        // $(this).prop("checked", true);
                    }

                    function arrHasDupes(A) {
                        // finds any duplicate array elements using the fewest possible comparison
                        var i, j, n;
                        n = A.length;
                   // to ensure the fewest possible comparisons
                        for (i = 0; i < n; i++) { // outer loop uses each item i at 0 through n
                            for (j = i + 1; j < n; j++) { // inner loop only compares items j at i+1 to n
                                if (A[i] == A[j])
                                    return true;
                            }
                        }
                        return false;
                    }

我需要将数组更改为

      var selectedItems = ["Group1:123", "Group1:564"] 
         if(group1[i] ==group1[j])
        {

        return group1[i];
}  

1 个答案:

答案 0 :(得分:0)

从问题和下面的评论中我真的不清楚你正在寻找什么结果,但是你说你需要知道哪些值是重复的,以及重复的值是什么,所以这是一种方法来做到这一点 - 根据需要进行调整:

如果您要比较的内容是字符串或很容易转换为字符串,我通常会使用一个标志对象,如下所示:

// Returns an object keyed by the "GroupX" string.
// Each entry is an array of the duplicate values
// for that key.
function findDuplicateGroups(a) {
  var singles = {};
  var duplicates = {};
  a.forEach(function(entry) {
    var parts, key, value, dups;
    
    // Get the parts of the entry
    // The `key` and `value` vars are just for clarity
    parts = entry.split(":");
    key = parts[0];
    value = parts[1];
    
    // Have we seen this key before?
    if (singles.hasOwnProperty(key)) {
      // Yes, get the duplicates array for the key if any
      dups = duplicates[key];
      if (!dups) {
        // New duplicate, add an entry with the two
        // values we've seen so far
        duplicates[key] = [singles[key], value];
      } else {
        // Existing duplicate, add this entry
        dups.push(value);
      }
    } else {
      // First entry we've seen for this key
      singles[key] = value;
    }
  });
  return duplicates;
}

// An array that does have duplicates
var yes = [
  "Group1:111",
  "Group2:111",
  "Group3:111",
  "Group1:222", // Duplicate in group 1
  "Group2:222", // Duplicate in group 2
  "Group1:333"  // Duplicate in group 1
];

var yesDups = findDuplicateGroups(yes);
var yesKeys = Object.keys(yesDups);
snippet.log("'yes' array has " + yesKeys.length + " dups");
yesKeys.forEach(function(key) {
  snippet.log("* " + key + ": " + yesDups[key].join(", "));
});

// An array that doesn't
var no = [
  "Group1:111",
  "Group2:111",
  "Group3:111"
];

var noDups = findDuplicateGroups(no);
var noKeys = Object.keys(noDups);
snippet.log("'no' array has " + noKeys.length + " dups");
noKeys.forEach(function(key) {
  snippet.log("* " + key + ": " + noDups[key].join(", "));
});
<!-- Script provides the `snippet` object, see http://meta.stackexchange.com/a/242144/134069 -->
<script src="http://tjcrowder.github.io/simple-snippets-console/snippet.js"></script>