This question already has an answer here:
这个问题在这里已有答案:
- Get all non-unique values (i.e.: duplicate/more than one occurrence) in an array 61 answers
- 获取阵列61答案中的所有非唯一值(即:重复/多次出现)
I'm looking for an easy way of removing a duplicate value from an array. I figured out how to detect if there is a duplicate or not, just I don't know how to "push" it from the value. For example, if you go to the link provided, and then type, "abca" (press return/enter key after each letter).. it will alert "duplicate!"
我正在寻找一种从数组中删除重复值的简单方法。我想出了如何检测是否有重复,只是我不知道如何从值中“推”它。例如,如果您转到提供的链接,然后键入“abca”(在每个字母后按回车键/输入键)..它将提醒“重复!”
But I also want to figure out how to remove that duplicate from the textarea?
但我还想弄清楚如何从textarea中删除该副本?
http://jsfiddle.net/P3gpp/
This is the part that seems to not be working ::
这是似乎无法正常工作的部分::
sort = sort.push(i);
textVal = sort;
return textVal;
6 个解决方案
#1
49
Why do it the hard way, it can be done more easily using javascript filter function which is specifically for this kind of operations:
为什么这么难,可以使用专门针对这种操作的javascript过滤功能更轻松地完成:
var arr = ["apple", "bannana", "orange", "apple", "orange"];
arr = arr.filter( function( item, index, inputArray ) {
return inputArray.indexOf(item) == index;
});
---------------------
Output: ["apple", "bannana", "orange"]
#2
4
These are the functions I created/use for removing duplicates:
这些是我创建/用于删除重复项的函数:
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;
};
So using the first one, this is how your code could look:
因此,使用第一个,这是您的代码的外观:
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");
}
DEMO: http://jsfiddle.net/VrcN6/1/
演示:http://jsfiddle.net/VrcN6/1/
#3
4
Based on user2668376 solution, this will return a new array without duplicates.
基于user2668376解决方案,这将返回一个没有重复的新数组。
Array.prototype.removeDuplicates = function () {
return this.filter(function (item, index, self) {
return self.indexOf(item) == index;
});
};
After that you can do:
之后你可以这样做:
[1, 3, 3, 7].removeDuplicates();
Result will be; [1, 3, 7]
.
结果将是; [1,3,7]。
#4
2
You can use Array.reduce()
to remove the duplicates. You need a helper object to keep track of how many times an item has been seen.
您可以使用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");
}
The output of the reduce()
step can be used directly to populate the text area again, without affecting the original sort order.
reduce()步骤的输出可以直接用于再次填充文本区域,而不会影响原始排序顺序。
演示
#5
1
You can do this easily with just an object:
只需一个对象即可轻松完成此操作:
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);
}
#6
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]
#1
49
Why do it the hard way, it can be done more easily using javascript filter function which is specifically for this kind of operations:
为什么这么难,可以使用专门针对这种操作的javascript过滤功能更轻松地完成:
var arr = ["apple", "bannana", "orange", "apple", "orange"];
arr = arr.filter( function( item, index, inputArray ) {
return inputArray.indexOf(item) == index;
});
---------------------
Output: ["apple", "bannana", "orange"]
#2
4
These are the functions I created/use for removing duplicates:
这些是我创建/用于删除重复项的函数:
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;
};
So using the first one, this is how your code could look:
因此,使用第一个,这是您的代码的外观:
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");
}
DEMO: http://jsfiddle.net/VrcN6/1/
演示:http://jsfiddle.net/VrcN6/1/
#3
4
Based on user2668376 solution, this will return a new array without duplicates.
基于user2668376解决方案,这将返回一个没有重复的新数组。
Array.prototype.removeDuplicates = function () {
return this.filter(function (item, index, self) {
return self.indexOf(item) == index;
});
};
After that you can do:
之后你可以这样做:
[1, 3, 3, 7].removeDuplicates();
Result will be; [1, 3, 7]
.
结果将是; [1,3,7]。
#4
2
You can use Array.reduce()
to remove the duplicates. You need a helper object to keep track of how many times an item has been seen.
您可以使用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");
}
The output of the reduce()
step can be used directly to populate the text area again, without affecting the original sort order.
reduce()步骤的输出可以直接用于再次填充文本区域,而不会影响原始排序顺序。
演示
#5
1
You can do this easily with just an object:
只需一个对象即可轻松完成此操作:
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);
}
#6
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]