使用Javascript删除多维数组中的重复行

时间:2014-08-11 13:18:58

标签: javascript

var MyArray = [
    [1, "07/28/2014"],
    [2, "07/29/2014"],
    [3, "07/28/2014"],
    [4, "07/30/2014"],
    [5, "07/28/2014"],
];

在此数组中,如何删除重复列并查找已删除项目的计数。

我需要对已移除的物品进行个人计数。

07/28/2014 is 2 times.

这是我的代码:

function(MyArray, search_val)
{
    var counter = 0; alert(MyArray.length);

    for(var i = 0; i < MyArray.length; i++) 
    {
        if(MyArray[i][0] == search_val)
        {   
             MyArray.splice(i, 1);
             counter++;
        }
    }
    alert("counter: "+counter);
    return counter;
}

2 个答案:

答案 0 :(得分:1)

以下是一些非常天真的基本解决方案。这两个示例仅用于演示目的。不要在生产代码中使用。有一些参数验证和其他检查,以简化示例。此外,根据您的浏览器要求或可用的框架,有更快更好的方法来进行相等性测试。将此视为更多研究的起点。

我会把它作为练习来改善这些答案。

对于您的数据,这将有效:

var count, isFirst, data = [[1,"07/28/2014"], [2,"07/29/2014"],[3, "07/28/2014"],[1,"07/28/2014"],[4, "07/30/2014"]];

count = 0
/* loop over each element of the array */
for(var x = 0; x < data.length; x++) {
    isFirst = true // used to skip the first matching element
    /* for each loop iteration, loop over every element in the array */
    for(var y = 0; y < data.length; y++) {
        /*
           check the inner loop element against the outer loop element
           to see if they satisfy your equality requirements.
           Notice the second set of index operator brackets, this is
           how you access the next dimension of the array.
        */
        if(data[x][1] === data[y][1]) {
            /*
               If this is not the first time we've found this match
               then this must be a duplicate element, so remove it
            */
            if (!isFirst) {
                data.splice(y, 1);
                count++;
            }

            isFirst = false // makes sure that future matches are removed
        }
    }
}

console.log(count);
console.log(data);

对于更通用的解决方案,一种可能性是将相等性测试作为匿名函数传递:

/* Note, this is the same algorithm as above */
function spliceIf(data, predicate) {
    var count = 0, isFirst;
    for(var x = 0; x < data.length; x++) {
        isFirst = true;
        for(var y = 0; y < data.length; y++) {
            if (predicate(data[x], data[y])) {
                if (!isFirst) {
                    data.splice(y, 1);
                    count++;
                }

                isFirst = false
            }
        }
    }
    return count;
}

var items = [[1,"07/28/2014"], [2,"07/29/2014"],[3, "07/28/2014"],[1,"07/28/2014"],[4, "07/30/2014"]];
// Now call the new function and pass in the equality test as the second parameter:
var itemsRemoved = spliceIf(items,
    function(a, b) {
        /*
            This predicate function will be passed to spliceIf. When it
            is called from within then spliceIf function, it will be 
            provided with the inner and outer elements of the loop.
            You can then do your equality test however you see fit.

            Notice the predicate function must return a value.

            This is equivalent to the "data[x][1] === data[y][1]" line
            in the example above.
        */
        return a[1] === b[1];
    }
);

console.log(itemsRemoved);
console.log(items);

答案 1 :(得分:0)

只是做了一些小提琴,我认为这可能会有所帮助。 JSFiddle of Duplicate Remover Function

var data = [
    [1, "07/28/2014"],
    [2, "07/29/2014"],
    [3, "07/28/2014"],
    [4, "07/30/2014"],
    [5, "07/28/2014"],
];
var data2 = [
    [1, "07/28/2014"],
    [2, "07/29/2014"],
    [3, "07/29/2014"],
    [4, "07/29/2014"],
    [5, "07/29/2014"],
];

    function removeDuplicates(Array){    
        this._newArray = [];
        this.numberOfDuplicates = 0;
        this.listDuplicates = [];
        //Remove Duplicates    
            for(i=0;i<Array.length;i++){      
                for(j=0;j<Array.length;j++){
                    if(!Array[i][1]) //skip the current loop if index is empty
                        break;
                    if(Array[i][1]==Array[j][1] && i!=j){
                        this.listDuplicates.push(Array[j]);                            
                        Array[j]=0;
                        this.numberOfDuplicates+=1; //increase counter for dupes
                    }
                }    
            }
        //Recreate Array

        this.newArray = function(){
            for(i=0;i<Array.length;i++){
                if(Array[i])
                    this._newArray.push(Array[i]);
            }
        return this._newArray;
        }
}

var data = new removeDuplicates(data);
console.log(data.newArray());
console.log(data.numberOfDuplicates + " Duplicates");
console.log(data.listDuplicates);

console.log("\n");
var data2 = new removeDuplicates(data2);
console.log(data2.newArray());
console.log(data2.numberOfDuplicates + " Duplicates");
console.log(data2.listDuplicates);