javascript 从逗号分隔的字符串中删除重复项

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/11417421/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-26 13:08:14  来源:igfitidea点击:

Remove Duplicates From Comma Delimited String

javascriptjquerycsvuniquecomma

提问by Tommy Arnold

How do I remove duplicates from a comma delimited string of integers using jquery/javascript?

如何使用 jquery/javascript 从逗号分隔的整数字符串中删除重复项?

My string is: 1,2,2,4,2,4,3,2,3,1,5,5,5,1,1,2

我的字符串是:1,2,2,4,2,4,3,2,3,1,5,5,5,1,1,2

采纳答案by biril

For one method, which doesn't demand writing the functionality from scratch or using some otherjs library check out duck-punching-with-jquery. (look for 'Example 2: $.unique() enhancement')

对于一种不需要从头开始编写功能或使用其他一些js 库的方法,请查看duck-punching-with-jquery。(查找“示例 2:$.unique() 增强”)

If you're willing to try some other library, the excellent underscorejsincludes a 'uniq' method which accomplishes just what you need.

如果您愿意尝试其他一些库,优秀的underscorejs包含一个 ' uniq' 方法,它可以完成您所需要的。

回答by John Wilson

function spit(){
           var answer = ("1,2,2,4,2,4,3,2,3,1,5,5,5,1,1,2").split(',').filter(function(item, pos,self) {
              return self.indexOf(item) == pos;
           });
}

回答by devang

Here's a simple example.

这是一个简单的例子。

var str1 = "a,b,c,a,d,e,b";
var characters = str1.split(",");
var distinctCharacters = [];
jQuery.each(characters, function(index, c) {
if (jQuery.inArray(c, distinctCharacters) > -1) {
        // do nothing
        alert("already exists " + c);
    } else {
        distinctCharacters.push(c);
    }
});
alert(distinctCharacters);?

回答by Waqar Janjua

From this post Remove Duplicates from JavaScript Array

来自这篇文章 从 JavaScript 数组中删除重复项

var names = ["Mike","Matt","Nancy","Adam","Jenny","Nancy","Carl"];
var uniqueNames = [];
$.each(names, function(i, el){
 if($.inArray(el, uniqueNames) === -1) uniqueNames.push(el);
  });

回答by mindandmedia

good answers, there is also _.uniq()from the library underscore.js

很好的答案,也有_.uniq()来自underscore.js的图书馆

回答by andy

you can create a function in your javascript as follows:

您可以在 javascript 中创建一个函数,如下所示:

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

then add .removeDuplicates()to the field you want the duplicates to be removed from after you split using the delimiter, such as:

然后.removeDuplicates()在使用分隔符拆分后添加到要从中删除重复项的字段,例如:

var item = $("#inInputTextbox").val().split(",").removeDuplicates();

in your case, if i understand correctly, it could simply be:

在你的情况下,如果我理解正确,它可能只是:

var item = [a,b,c,d,d,d,c,b,a].split(",").removeDuplicates();

hope this helps.

希望这可以帮助。

回答by Damien Locque

var numbersString = "1,2,3,4,5,6";
var numbersArray = numbers.split(',');

Now just insert with check in another array the content of numbersArray

现在只需在另一个数组中插入检查内容 numbersArray

回答by ZER0

If they are only integers, you could have something like that:

如果它们只是整数,你可以有这样的东西:

var array = "1,2,2,4,2,4,3,2,3,1,5,5,5,1,1,2".split(",");

array.sort(function(a, b) { return a - b });

var uniqueArray = [array[0]];

for (var i = 1; i < array.length; i++) {
    if (array[i - 1] !== array[i])
        uniqueArray.push(array[i]);
}

console.log(uniqueArray);