Javascript:在数组中搜索重复项

发布于 2024-12-25 07:52:12 字数 1103 浏览 1 评论 0 原文

这就是我正在尝试做的: 我向用户提供一个文本区域,他必须输入一些域,如果他输入同一域两次(重复),我想删除这些重复项。

到目前为止,我已经走到了可以找到骗子的部分,这是我正在使用的代码:

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;
}

任何更好地做到这一点的建议将不胜感激,而且我也不知道如何取出骗子:(
谷歌搜索我发现我可能必须使用“拼接”,但不太确定。

预先感谢!
R

(PS 抱歉,格式看起来很奇怪,但是当我粘贴代码时发生了这种情况)

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.

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

(P.S Sorry the format looks weird, but that happened when I pasted in the code)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

若能看破又如何 2025-01-01 07:52:12

这是我的微弱尝试。它类似于

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;
}

该对象也将包含每个对象有多少个重复项。 arrCopy 具有唯一的值。

编辑:请参阅 RobG 关于 hasOwnProperty 的评论。在这种情况下应该是

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

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.

EDIT: see RobG's comment concerning hasOwnProperty. In this case it should be

...
    if(!list.hasOwnProperty(arr[i]))
...
烛影斜 2025-01-01 07:52:12

很多答案。这是一个使用通用函数来创建一组唯一成员的函数。请注意,结果将被排序,使用对象并使用 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;
}

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.

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;
}
顾冷 2025-01-01 07:52:12

您可以使用称为关联数组的东西来解决这个问题。看看它是否适合你。

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);

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);
一花一树开 2025-01-01 07:52:12

第一名

复制数组,第2 个小
数组在javascript中有一个“排序”功能,我建议你在比较

第三个 之前对其进行排序
我看到你正在使用冒泡排序,这很好。在 for(j=0;j 中,你不需要从 0 开始,你可以从 i

最后开始,使用已经存在的东西,http://api.jquery.com/jQuery.unique/

1st
Copy array, and lowercase it first

2nd
Array has a "sort" function in javascript, I suggest you sort it before compare

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

last, use something already there, http://api.jquery.com/jQuery.unique/

嘿看小鸭子会跑 2025-01-01 07:52:12

我认为这里有一个错误..

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

这段代码不应该是?

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

注意 j=i+1newtext.length -1 (最后一个是可选的)

然后:

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

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)

Then:

  if(newtext[j].toLowerCase()==newtext[i].toLowerCase())
      return 'dupe';
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文