17

我正在寻找一种从数组中删除重复值的简单方法。我想出了如何检测是否有重复,只是我不知道如何从值中“推送”它。例如,如果您转到提供的链接,然后键入“abca” (每个字母后按回车/回车键) ......它会提示“重复!”

但我也想弄清楚如何从 textarea 中删除该重复项?

http://jsfiddle.net/P3gpp/

这是似乎不起作用的部分::

sort = sort.push(i);
textVal = sort;
return textVal;
4

6 回答 6

69

为什么要这样做,使用专门针对此类操作的javascript过滤器功能可以更轻松地完成:

var arr = ["apple", "bannana", "orange", "apple", "orange"];

arr = arr.filter( function( item, index, inputArray ) {
           return inputArray.indexOf(item) == index;
    });


---------------------
Output: ["apple", "bannana", "orange"]
于 2013-08-20T06:09:29.433 回答
6

基于 user2668376 解决方案,这将返回一个没有重复的新数组。

Array.prototype.removeDuplicates = function () {
    return this.filter(function (item, index, self) {
        return self.indexOf(item) == index;
    });
};

之后,您可以执行以下操作:

[1, 3, 3, 7].removeDuplicates();

结果将是;[1, 3, 7].

于 2014-07-02T10:27:51.540 回答
4

这些是我创建/用于删除重复项的功能:

var removeDuplicatesInPlace = function (arr) {
    var i, j, cur, found;
    for (i = arr.length - 1; i >= 0; i--) {
        cur = arr[i];
        found = false;
        for (j = i - 1; !found && j >= 0; j--) {
            if (cur === arr[j]) {
                if (i !== j) {
                    arr.splice(i, 1);
                }
                found = true;
            }
        }
    }
    return arr;
};

var removeDuplicatesGetCopy = function (arr) {
    var ret, len, i, j, cur, found;
    ret = [];
    len = arr.length;
    for (i = 0; i < len; i++) {
        cur = arr[i];
        found = false;
        for (j = 0; !found && (j < len); j++) {
            if (cur === arr[j]) {
                if (i === j) {
                    ret.push(cur);
                }
                found = true;
            }
        }
    }
    return ret;
};

所以使用第一个,这就是你的代码的样子:

function cleanUp() {
    var text = document.getElementById("fld"),
        textVal = text.value,
        array;

    textVal = textVal.replace(/\r/g, " ");
    array = textVal.split(/\n/g);

    text.value = removeDuplicatesInPlace(array).join("\n");
}

演示:http: //jsfiddle.net/VrcN6/1/

于 2013-08-02T03:01:00.220 回答
2

您可以使用Array.reduce()删除重复项。您需要一个辅助对象来跟踪一个项目被看到的次数。

function cleanUp() 
{
    var textBox = document.getElementById("fld"),
    array = textBox.value.split(/\r?\n/g),
    o = {},
    output;

    output = array.reduce(function(prev, current) {
        var key = '$' + current;

        // have we seen this value before?
        if (o[key] === void 0) {
            prev.push(current);
            o[key] = true;
        }

        return prev;
    }, []);

    // write back the result
    textBox.value = output.join("\n");
}

reduce()步骤的输出可直接用于再次填充文本区域,而不会影响原始排序顺序。

演示

于 2013-08-02T03:09:44.710 回答
1

您只需一个对象即可轻松完成此操作:

function removeDuplicates(text) {
    var seen = {};
    var result = '';

    for (var i = 0; i < text.length; i++) {
        var char = text.charAt(i);

        if (char in seen) {
            continue;
        } else {
            seen[char] = true;
            result += char;
        }
    }

    return result;
}

function cleanUp() {
    var elem = document.getElementById("fld");

    elem.value = removeDuplicates(elem.value);
}
于 2013-08-02T03:02:15.617 回答
0
arr3 = [1, 2, 3, 2, 4, 5];
unique = [];

function findUnique(val)
{
  status = '0';  
  unique.forEach(function(itm){
    if(itm==val){ 
      status=1;
    }
  })
  return status;
}

arr3.forEach(function(itm){
  rtn =  findUnique(itm);
  if(rtn==0)
    unique.push(itm);
});

console.log(unique);  // [1, 2, 3, 4, 5]
于 2014-05-17T16:14:35.393 回答