简体   繁体   English

从2d数组中删除重复项

[英]Removing duplicates from 2d array

 var result = [ [0, 0, 0, -2], [0, 0, -2, 0], [0, 0, 0, -2], [0, 0, -2, 0], [0, -2, 0, 0], [0, -2, 0, 0], [0, 0, 0, -2], [0, 0, -2, 0], [0, 0, 0, -2], [0, 0, -2, 0], [0, -2, 0, 0], [0, -2, 0, 0], [0, 0, 0, -2], [0, 0, -2, 0], [0, 0, 0, -2], [0, 0, -2, 0], [0, -2, 0, 0], [0, -2, 0, 0], [-2, 0, 0, 0], [-2, 0, 0, 0], [-2, 0, 0, 0], [-2, 0, 0, 0], [-2, 0, 0, 0], [-2, 0, 0, 0] ]; for (var i = 0; i < result.length; i++) { //remove duplicates var listI = result[i]; loopJ: for (var j = 0; j < result.length; j++) { var listJ = result[j]; //listJ and listI point at different arrays within the result array if (listI === listJ) continue; //Ignore itself for (var k = listJ.length; k >= 0; k--) { //checks whether the values are different, if they are continue with the loop if (listJ[k] !== listI[k]) continue loopJ; } // At this point, their values are equal so we remove from the result array result.splice(j, 1); } } document.getElementById("result").innerHTML = JSON.stringify(result); 
 <div id="result"> </div> 

I'm trying to remove duplicates from some data my program creates when it permutes a 2d array, but in some cases it fails to remove all duplicates. 我正在尝试从我的程序在置换二维数组时创建的某些数据中删除重复项,但在某些情况下,它无法删除所有重复项。 I'm a bit lost as to why, the code is short and commented, I've hard coded in the input 2d array under the result variable. 我有点迷失为什么,代码很短并且注释,我在结果变量下的输入2d数组中进行了硬编码。

https://jsfiddle.net/h26ro89p/2/ https://jsfiddle.net/h26ro89p/2/

You forgot to backtrack one index when performing the splice: 您在执行拼接时忘记了回溯一个索引:

    result.splice(j, 1);
    j--;

To prevent this in the future you could reverse the loop or even use a while loop. 为了防止将来出现这种情况,您可以反转循环甚至使用while循环。 With a bit of old heroic google searching you can find a solution here: 通过一些古老的英雄谷歌搜索,你可以在这里找到一个解决方案:

Remove items from array with splice in for loop 使用splice in for循环从数组中删除项目

i will add something shorter with lodash 我将用lodash添加更短的东西

https://jsfiddle.net/h26ro89p/3/ https://jsfiddle.net/h26ro89p/3/

        var result = [[0,0,0,-2],[0,0,-2,0],[0,0,0,-2],[0,0,-2,0],[0,-2,0,0],[0,-2,0,0],[0,0,0,-2],[0,0,-2,0],[0,0,0,-2],[0,0,-2,0],[0,-2,0,0],[0,-2,0,0],[0,0,0,-2],[0,0,-2,0],[0,0,0,-2],[0,0,-2,0],[0,-2,0,0],[0,-2,0,0],[-2,0,0,0],[-2,0,0,0],[-2,0,0,0],[-2,0,0,0],[-2,0,0,0],[-2,0,0,0]];


document.getElementById("result").innerHTML = JSON.stringify(_.uniqWith(result, _.isEqual));

Check this fiddle https://jsfiddle.net/c2uk0xow/3/ , you'll need JQuery to use $.each function. 检查这个小提琴https://jsfiddle.net/c2uk0xow/3/ ,你需要JQuery来使用$ .each函数。

var arr = [
      [0, 0, 0, -2],
      [0, 0, -2, 0],
      [0, 0, 0, -2],
      [0, 0, -2, 0],
      [0, -2, 0, 0],
      [0, -2, 0, 0],
      [0, 0, 0, -2],
      [0, 0, -2, 0],
      [0, 0, 0, -2],
      [0, 0, -2, 0],
      [0, -2, 0, 0],
      [0, -2, 0, 0],
      [0, 0, 0, -2],
      [0, 0, -2, 0],
      [0, 0, 0, -2],
      [0, 0, -2, 0],
      [0, -2, 0, 0],
      [0, -2, 0, 0],
      [-2, 0, 0, 0],
      [-2, 0, 0, 0],
      [-2, 0, 0, 0],
      [-2, 0, 0, 0],
      [-2, 0, 0, 0],
      [-2, 0, 0, 0]
    ];
    var _tmp_arr = [];
    var result = [];
    $.each(arr, function(i,v)
    {
        if(!_tmp_arr.includes(JSON.stringify(v)))
        {
            _tmp_arr.push(JSON.stringify(v));
        }
    });

    $.each(_tmp_arr, function(i,v)
    {
        result.push(JSON.parse(v));
    });

Note : I had to convert to string and then parse again, in order to compare both results. 注意 :我必须转换为字符串,然后再次解析,以便比较两个结果。 [-2,0,0,0] === [-2,0,0,0] returned false, but after I stringify it it gave true, "[-2,0,0,0]" === "[-2,0,0,0]" . [-2,0,0,0] === [-2,0,0,0]返回false,但在我将其字符串化后,它给出了真, "[-2,0,0,0]" === "[-2,0,0,0]"

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM