Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/422.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/82.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
javascript合并相同的数组元素_Javascript_Jquery_Arrays - Fatal编程技术网

javascript合并相同的数组元素

javascript合并相同的数组元素,javascript,jquery,arrays,Javascript,Jquery,Arrays,我有这个数组:我想知道如何删除重复的数组元素,而不考虑数组中元素的位置 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]] 提前谢谢 对不起,到目前为止我还有这个 newArrayVal =

我有这个数组:我想知道如何删除重复的数组元素,而不考虑数组中元素的位置

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]]
提前谢谢

对不起,到目前为止我还有这个

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

您可以通过循环中的循环来实现这一点:

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]]
这应该可以完成这项工作,希望它有助于: 我把它放在JSFIDLE上:

在本例中,相等的是检查条件是否适用的函数

请注意,此解决方案也适用于包含字符串而不是数字的数组,因为我这里没有使用Sly_cardinal在其解决方案中使用的排序函数


我认为kennebec的解决方案更好地回答了这个问题,因为它更干净,并且使用了新的Array.prototype.filter函数

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]]
这应该可以完成这项工作,希望它有助于: 我把它放在JSFIDLE上:

在本例中,相等的是检查条件是否适用的函数

请注意,此解决方案也适用于包含字符串而不是数字的数组,因为我这里没有使用Sly_cardinal在其解决方案中使用的排序函数


我认为kennebec的解决方案更能回答这个问题,因为它更干净,并且使用了新的Array.prototype.filter函数

基于数学和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;
}

根据数学和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;
}

您可以对内部阵列进行排序和合并,以便于查找

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=数组[[0,1],[2,3],[4,5]*/

您可以对内部数组进行排序和合并,以便于查找

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=数组[[0,1],[2,3],[4,5]*/

我将从一个循环开始。编辑:我读错了,你可能只需要使用.filter。我会首先更改内部数组,以便它们都在内部排序。然后,在数组中循环,在每次迭代中重新循环数组,并删除与当前数组相同的元素。谢谢大家所有答案都有效,我将标记为答案谢谢,againI将以循环开始。编辑:我读错了,你可能只需要使用.filter。我会首先更改内部数组,以便它们都在内部排序。然后,在数组中循环,在每次迭代中重新循环数组,并删除与当前数组相同的任何元素。谢谢大家所有答案都有效,我将标记为第一个答案再次感谢