我如何在javascript对象中查找和计算重复值

时间:2017-09-20 11:08:35

标签: javascript jquery arrays

我有以下数组。是否有数组的console.log,如下所示

(3) [Array(4), Array(4), Array(4)]
0 ["jackpot", "cherry", "bar", "pear"]
1 ["raspberry", "raspberry", "raspberry", "lemon"]
2 ["plum", "crown", "lemon", "jackpot"]

我尝试了下面的内容,在控制台中我只看到我的数组和旁边的1

var counts = {};
currentResults.forEach(function(obj) {
var key = JSON.stringify(obj)
counts[key] = (counts[key] || 0) + 1
})
console.log(counts);

如何在上面的数组/对象中搜索和查找重复的值,并使用javascript / jquery计算我有多少重复的项目?

4 个答案:

答案 0 :(得分:1)

您可以使用解决方案



var data = [["jackpot", "cherry", "bar", "pear"], ["raspberry", "raspberry", "raspberry", "lemon"],["plum", "crown", "lemon", "jackpot"]];

var key = {};
for(var i=0; i<data.length; i++){
  for(var j=0; j<data[i].length; j++){
    if(typeof key[data[i][j]] === 'undefined'){
      key[data[i][j]] = 1;
    } else {
      key[data[i][j]] = parseInt(key[data[i][j]]) + 1;
    }
  }
}

console.log(key);
&#13;
&#13;
&#13;

我假设你的数据是一个数组数组。循环通过阵列和放大器对于每个项目,以JSON(键:值)的形式保持计数。

Key as item&amp;价值就是数量。

更新了解决方案以获取重复项

&#13;
&#13;
var data = [["jackpot", "cherry", "bar", "pear"], ["raspberry", "raspberry", "raspberry", "lemon"],["plum", "crown", "lemon", "jackpot"]];

var key = {};
var duplicate = [];
for(var i=0; i<data.length; i++){
	for(var j=0; j<data[i].length; j++){
  	if(typeof key[data[i][j]] === 'undefined'){
    	key[data[i][j]] = 1;
    } else {
    	key[data[i][j]] = parseInt(key[data[i][j]]) + 1;
      if(parseInt(key[data[i][j]]) >= 3 && duplicate.indexOf(data[i][j]) == -1){
      	duplicate.push(data[i][j]);
      }
    }
  }
}

console.log(duplicate);
&#13;
&#13;
&#13;

更新了在子数组中查找重复项的解决方案

&#13;
&#13;
var data = [["jackpot", "cherry", "bar", "pear"], ["raspberry", "raspberry", "raspberry", "lemon"],["plum","jackpot", "crown", "lemon", "jackpot", "jackpot"]];

var key = {};
var duplicate = [];
for(var i=0; i<data.length; i++){
	key = {};
	for(var j=0; j<data[i].length; j++){
  	if(typeof key[data[i][j]] === 'undefined'){
    	key[data[i][j]] = 1;
    } else {
    	key[data[i][j]] = parseInt(key[data[i][j]]) + 1;
      if(parseInt(key[data[i][j]]) >= 3 && duplicate.indexOf(data[i][j]) == -1){
      	duplicate.push(data[i][j]);
      }
    }
  }
}

console.log(duplicate);
&#13;
&#13;
&#13;

希望这会对你有所帮助。

答案 1 :(得分:0)

使用这个:)

var test=[["jackpot", "cherry", "bar", "pear"], ["raspberry", "raspberry", "raspberry", "lemon"],["plum", "crown", "lemon", "jackpot"]];
var counts = {};
for(var i=0;i<test.length;i++)
test[i].forEach((x)=> { counts[x] = (counts[x] || 0)+1; });
console.log(counts);

enter image description here

答案 2 :(得分:0)

您可以迭代主数组中的每个数组元素并在新数组中推送唯一值,如果该值已经存在,则重复并将其存储在结果数组中。

这里我没有编写整个代码,因为我看不到你尝试过的东西。

您可以使用此方法检查新数组中是否已存在元素

arr.indexOf()

修改 更好的方法是迭代主数组中的每个数组元素,并将它们存储为对象的属性,并将它们的计数存储为属性的值。 最后你的对象是这样的:

{
  "raspberry": 3,
  "lemon" : 2
}

答案 3 :(得分:0)

您可以检查每个阵列的重复,也可以在一个阵列中合并阵列以检查所有阵列。

        var array1 = ["jackpot", "cherry", "bar", "pear"];
        var array2 = ["raspberry", "raspberry", "raspberry", "lemon"];
        var array3 = ["plum", "crown", "lemon", "jackpot"];

        var parentArray = [array1, array2, array3];
        var joinedArray = [];

        // ForEach Array
        var duplicatedElementsForEachArray = [];
        parentArray.forEach(function (childArray) {

            // Merge Arrays in joinedArray
            joinedArray = joinedArray.concat(childArray);

            var duplicatedElements = 0;
            var sortedChildArray = childArray.slice().sort(); // Sorting each Arrays
            var i = 0, l = sortedChildArray.length - 1;
            while (i < l) { if (sortedChildArray[i] == sortedChildArray[i + 1]) duplicatedElements++; i++; } // If i. and (i+1). elements are same, this will be duplication
            duplicatedElementsForEachArray.push(duplicatedElements);
            console.log(duplicatedElements);
        });

        // Joined Array, you can check duplications for merged array with same algorithm
        var sortedJoinedArray = joinedArray.slice().sort();
        var duplicatedElementsForJoinedArray = 0;
        var i = 0, l = sortedJoinedArray.length - 1;
        while (i < l) { if (sortedJoinedArray[i] == sortedJoinedArray[i + 1]) duplicatedElementsForJoinedArray++; i++; }
        console.log(duplicatedElementsForJoinedArray);