简体   繁体   English

Javascript:在数组中搜索重复项

[英]Javascript: searching within an array for dupes

This is what I am trying to do: I present the user with a textarea and he must enter some domains, if he enters the same domain twice (a duplicate) I want to delete the dupes. 这就是我想要做的:我向用户显示一个textarea,如果他两次输入相同的域(重复),我必须输入一些域,我想删除重复对象。

So far I have come till the part where I can find the dupes, this is the code I am using: 到目前为止,我一直到可以找到重复对象的地方,这是我正在使用的代码:

function check_if_already_in_the_list___manual_textbox()
{

var therows=0;
var thetext = document.forms[0].text.value;
var newtext = thetext.split("\n");
therows+=newtext.length;
var i;
var match_counter=0;

    for(i=0;i<newtext.length;i++) // first iterate over the number of items
    {
        for(j=0;j<newtext.length;j++) // second, start a second loop to compare each other
        {

            if(newtext[j].toLowerCase()==newtext[i].toLowerCase())
            {           
            match_counter++;
            }

        if(match_counter >=2) // Found dupe!
        {alert("Matched:"+newtext[j]+" "+newtext[i]+" Counter"+match_counter);
match_counter=0;}


        }
        alert("Match counter:"+match_counter+ " D:"+newtext[i]);'
match_counter=0;
    }
//alert(""+match_counter);
return match_counter;
}

Any suggestions to do this better would be most appreciated, also I have no idea how to take out the dupes :( 任何建议,以更好地做到这一点将不胜感激,也我不知道如何取出骗子:(
Googling I see that I probably have to use "splice" but not really sure. 谷歌搜索我看到我可能必须使用“拼接”,但不是很确定。

Thanks in advance! 提前致谢!
R [R

(PS Sorry the format looks weird, but that happened when I pasted in the code) (对不起,格式看起来很奇怪,但是在我粘贴代码时就发生了)

Here's my feeble attempt at it. 这是我微不足道的尝试。 It's similar to 类似于

var arr = ["1","2","3","4","5","3","2","3","4"];
var arrCopy = [];
var list = {};

for (var i = 0, len = arr.length; i < len; i++) {
    if(!list[arr[i]])
        arrCopy.push(arr[i]);
    list[arr[i]] = ++list[arr[i]] | 0;
}

The object will also contain how many duplicates there were for each one. 该对象还将包含每个对象有多少个副本。 arrCopy has the unique values. arrCopy具有唯一值。

EDIT: see RobG's comment concerning hasOwnProperty . 编辑:参见RobG关于hasOwnProperty的评论。 In this case it should be 在这种情况下,应该

...
    if(!list.hasOwnProperty(arr[i]))
...

Lots of answers. 很多答案。 Here's one that uses a generic funciton to create an array of unique members. 这是一个使用通用函数创建一组唯一成员的数组。 Note that the result will be sorted, using an object and getting back properties using for..in isn't guaranteed to maintain any particular order. 注意,将使用对象对结果进行排序,并不能保证使用for..in返回属性以保持任何特定顺序。

var el = document.forms[0].text;
el.value = unique(el.value.toLowerCase().split(/[\n\r]/)).join('\n');

function unique(arr) {
  arr.sort();
  var i = arr.length;

  while (i--) {
    if (arr[i] == arr[i - 1]) {
       arr.splice(i, 1);
    }
  }
  return arr;
}

You can make use of something called as associative arrays to solve this problem. 您可以利用称为关联数组的方法来解决此问题。 See if it works for you. 看看是否适合您。

var initial_array = ['www.yahoo.com', 'www.google.com', 'www.facebook.com', 'www.google.com'];
var set = {};
for (var domain in initial_array){
    set[initial_array[domain].toLowerCase()] = true;
}
alert(set);

1st Copy array, and lowercase it first 第一个Copy数组,先小写

2nd Array has a "sort" function in javascript, I suggest you sort it before compare 第二数组在javascript中具有“排序”功能,建议您在比较之前对其进行排序

3rd I saw you are using bubble sorting, it's good. 第三,我看到您正在使用气泡排序,这很好。 At for(j=0;j<newtext.length;j++) , you do not need to start from 0, you can start from i for(j=0;j<newtext.length;j++) ,您不需要从0开始,可以从i开始

last, use something already there, http://api.jquery.com/jQuery.unique/ 最后,使用已经存在的内容http://api.jquery.com/jQuery.unique/

I think there is a mistake here.. 我认为这是一个错误。

for(i=0;i<newtext.length;i++) // first iterate over the number of items
{
    for(j=0;j<newtext.length;j++) // second, start a second loop to compare each other

This code should not be?? 此代码不应该是??

for(i=0;i<newtext.length -1;i++) // first iterate over the number of items
{
    for(j=i+1;j<newtext.length;j++) // second, start a second loop to compare each other

note j=i+1 and newtext.length -1 (this last one is optional) 注意j=i+1newtext.length -1 (这最后一个是可选的)

Then: 然后:

  if(newtext[j].toLowerCase()==newtext[i].toLowerCase())
      return 'dupe';

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM