var arr = [1, 1, 1, 5, 3, 4, 6, 6]
function uniqueRetriever(bla, boi) {
...
}
var unique = uniqueRetriever(bla, boi)
console.log(unique);
//output: [5, 3, 4]
如何在不更改原始数组的情况下从数组中检索唯一元素?
答案 0 :(得分:1)
尝试filter()
,如下所示:
var arr = [1, 1, 1, 5, 3, 4, 6, 6];
var res = arr.filter(function (item, index, arr) {
var count = 0;
for(var i = 0; i < arr.length; ++i){
if(arr[i] == item)
count++;
}
// check if item appears once then return item
if(count == 1){
return arr.indexOf(item) === index;
}
})
console.log(res);