2017-05-04 60 views
1

我試圖從數組列表中刪除重複項。我試圖這樣做的方式是使用reduce創建一個空數組,將所有未定義的索引推送到該數組上。我得到的錯誤雖然這使用reduce刪除數組中的重複項

if(acc[item]===undefined){ 
    ^
TypeError: Cannot read property '1' of undefined 

我下面的功能:

function noDuplicates(arrays) { 
 
    var arrayed = Array.prototype.slice.call(arguments); 
 

 
    return reduce(arrayed, function(acc, cur) { 
 
    forEach(cur, function(item) { 
 
     if (acc[item] === undefined) { 
 
     acc.push(item); 
 
     } 
 
     return acc; 
 
    }); 
 
    }, []); 
 
} 
 

 
console.log(noDuplicates([1, 2, 2, 4], [1, 1, 4, 5, 6]));

+0

應該輸出什麼3210 console.log(noDuplicates([1,2,2,4],[1,1,4,5,6]));? – Ved

+0

輸出應該是[1,2,4,5,6] – learninjs

+0

[從JavaScript數組中刪除重複]的可能副本(http://stackoverflow.com/questions/9229645/remove-duplicates-from-javascript-array) –

回答

2

有許多的問題與你是如何調用方法和你來自哪裏,返回ACC

function noDuplicates(arrays) { 
 
    var arrayed = Array.prototype.slice.call(arguments); 
 

 
    // reduce is a method of an array, so call it as a method 
 
    // return reduce(arrayed, function(acc, cur) { 
 
    return arrayed.reduce(function(acc, cur) { 
 
    
 
    // Same with forEach 
 
    cur.forEach(function(item) { 
 
     if (acc[item] === undefined) { 
 
     acc.push(item); 
 
     } 
 
     // Return acc from the reduce callback, forEach returns undefined always 
 
     // return acc; 
 
    }); 
 
    return acc; 
 
    }, []); 
 
} 
 

 
console.log(noDuplicates([1, 2, 2, 4], [1, 1, 4, 5, 6]));

你也可以撥打直接減少參數使用電話:

Array.prototype.reduce.call(arguments, function(acc, curr) { 
    // ... 
}); 

以上,使你的代碼運行,但它不會產生正確的輸出作爲測試:

if (acc[item] === undefined) 

你想要什麼沒有做。你需要做的是記住每個值,而只把它推到ACC如果它以前沒有見過:

function noDuplicates(arrays) { 
 
    var arrayed = Array.prototype.slice.call(arguments); 
 
    var seen = {}; 
 

 
    return arrayed.reduce(function(acc, cur) { 
 
    cur.forEach(function(item) { 
 
     if (!seen[item]) { 
 
     acc.push(item); 
 
     seen[item] = true; 
 
     } 
 
    }); 
 
    return acc; 
 
    }, []); 
 
} 
 

 
console.log(noDuplicates([1, 2, 2, 4], [1, 1, 4, 5, 6]));

一些其他的方法:

// A more concise version of the OP 
 
function noDupes() { 
 
    return [].reduce.call(arguments, function(acc, arr) { 
 
    arr.forEach(function(value) { 
 
     if (acc.indexOf(value) == -1) acc.push(value); 
 
    }); 
 
    return acc; 
 
    },[]); 
 
} 
 
    
 
console.log(noDupes([1, 2, 2, 4], [1, 1, 4, 5, 6])); 
 

 
// Some ECMAScript 2017 goodness 
 
function noDupes2(...args){ 
 
return [].concat(...args).filter((v, i, arr) => arr.indexOf(v)==i); 
 
} 
 

 
console.log(noDupes2([1, 2, 2, 4], [1, 1, 4, 5, 6]));

4

首先將兩者連接起來陣列,下次使用filter()過濾出獨特的物品─

var a = [1, 2, 2, 4], b = [1, 1, 4, 5, 6]; 
 
var c = a.concat(b); 
 
var d = c.filter(function (item, pos) {return c.indexOf(item) == pos}); 
 
console.log(d);

3

任何使用原因reduce?因爲我們可以通過首先合併這兩個arrays,然後使用Set刪除重複的密鑰來輕鬆完成此操作。

檢查:

function noDuplicates(a, b){ 
 
    var k = a.concat(b); 
 
    return [...new Set(k)]; 
 
} 
 

 
console.log(noDuplicates([1,2,2,4],[1,1,4,5,6]));

檢查DOC,how Set works