简体   繁体   English

javascript合并相同的数组元素

[英]javascript merge array elements that are the same

I have this array: I was wondering how can I remove the duplicate arrays elements regardless of the position of the elements inside the array 我有这个数组:我想知道如何删除重复的数组元素,而不管元素在数组中的位置如何

example
array1 = [[0, 1], [1, 0], [2, 3], [3, 2], [4, 5], [5, 4]];
//the [0,1],[1,0] will be counted as the same element, so as the others..
//to
array1 = [[0, 1], [2, 3], [4, 5]]

thanks in advance 提前致谢

sorry i have this so far 对不起,我到目前为止

newArrayVal = [];

arr1=  [[0, 1], [1, 0], [2, 3], [3, 2], [4, 5], [5, 4]];
for(i=0; i<arr1.length; i++){
  newArrayVal[i] = arr1[i].sort();
}
console.log(newArrayVal);
//thanks frenchie I think i am near to the solution, i just need to filter this now, hehe thanks

You could do that with a loop in a loop: 您可以在循环中执行以下操作:

array1 = [[0, 1], [1, 0], [2, 3], [3, 2], [4, 5], [5, 4]];

function equal(a1, a2) {
   return (a1[0]==a2[0] && a1[1]==a2[1]) || (a1[0]==a2[1] && a1[1]==a2[0]);
}

for(var i=0; i<array1.length; i++) {
    for(var j=i+1; j<array1.length; j++) {
        if(equal(array1[i], array1[j])) {
            array1.splice(j,1);
        }
    }
}

// array1 = [[0,1],[2,3],[4,5]]

This should do the job, hope it helps :) I put it on jsfiddle: http://jsfiddle.net/eu1v8eso/ 这应该可以完成工作,希望对您有所帮助:)我把它放在jsfiddle上: http : //jsfiddle.net/eu1v8eso/

In this example equal() is the function checking if the conditions apply. 在此示例中,equal()是检查条件是否适用的函数。

Note that this solution also works with an array containing strings instead of numbers as I'm not using a sort function here (which Sly_cardinal does in his solution) 请注意,此解决方案还适用于包含字符串而不是数字的数组,因为我在这里不使用排序功能(Sly_cardinal在其解决方案中这样做)

I think kennebec's solution is the better answer to this question as it is much cleaner and uses the new Array.prototype.filter function 我认为肯纳贝克的解决方案是此问题的更好答案,因为它更清洁并且使用了新的Array.prototype.filter函数

Based on Mathletics and frenchie's comments, here is a solution: 根据Mathletics和Frenchie的评论,这是一个解决方案:

/**
 * Returns the unique sub-lists of the given list:
 * 
 * e.g. 
 * given:
 *     getUnique([[0, 1], [1, 0], [2, 3], [3, 2], [4, 5], [5, 4]])
 * 
 * returns:
 * 
 *     [[0, 1], [2, 3], [4, 5]]
 * 
 * @param  {number[][]} list List of array of numbers
 * @return {number[][]} Returns a list of unique sub-arrays
 */
function getUnique(list){

    var numSort = function(a, b){
        return a - b;
    };

    var getItemId = function(list){
        return list.join(',');
    };

    // Map each sub-list to a unique ID.
    // Keep track of whether we've seen that
    // ID before and only keep the first list
    // with that ID.
    var uniqueMap = {};
    var resultList = list.filter(function(item, index){
        var keepItem = false;
        var itemId = getItemId(item.sort(numSort));
        if (!uniqueMap[itemId]){
            uniqueMap[itemId] = true;
            keepItem = true;
        }
        return keepItem;
    });

    return resultList;
}

You can sort and join the interior arrays for an easier lookup. 您可以对内部阵列进行排序和合并,以简化查找。

function uniqueDeepArray(A){
    var next, b= A.map(function(itm){
        return itm.sort().join('');
    });
    return A.filter(function(itm, i){
        return b.indexOf(b[i])== i;
    });
}



var a1= [[0, 1], [1, 0], [2, 3], [3, 2], [4, 5], [5, 4]],
a2= uniqueDeepArray(a1);    

/* a2= (Array) [[0,1],[2,3],[4,5]] */ / * a2 =(数组)[[0,1],[2,3],[4,5]] * /

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

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