【问题标题】:Search, count duplicated strings and add for each numbers搜索,计算重复的字符串并为每个数字添加
【发布时间】:2017-01-15 00:20:47
【问题描述】:

我有一个脚本在数组中搜索重复的文本字符串并更改颜色。

function checkDuplicates() {
    var values = new Array();
    var $input = $('input[type=\'text\']');
    var error = 0;
    $input.each(function() {
        $(this).removeClass('double-error');
        var that = this;
        if (that.value!='') {
            values[that.value] = 0;
            $('input[type=\'text\']').each(function() {
                if (this.value == that.value) {
                    values[that.value]++;
                    }
                });
            } //endif
        });

    $input.each(function(key) {
        if (values[this.value]>1) {
            error++;
            $(this).addClass('double-error');
            }
        });

    return (error <= 0); //returns false or true
    }

<style type="text/css">
    .double-error {
        color:red;
        border:1px solid red;
        }
</style>

一切正常。


但是,我需要计算重复的字符串并添加跟踪它们是该单词的第一次出现,该单词的第二次出现,等等。

例如:

给定john, john, peter, doe, peter, john,结果将是john-1, john-2, peter-1, doe, peter-2, john-3


这是我目前拥有的:

function eliminateDuplicates() {
    var values = new Array();
    var $input = $('input[type=\'text\']');
    var error = 0;

    $input.each(function() {
        $(this).removeClass('double-error');
        var that = this;
        if (that.value!='') {
            values[that.value] = 0;
            $('input[type=\'text\']').each(function() {
                if (this.value == that.value) {
                    values[that.value]++;
                }
            });
        }
    });

    $input.each(function(key) {
        if (values[this.value]>1) {
            error++;
            myArray = values[this.value];
            for (var i = 0; i < myArray; i++) {
                $(this).parent()
                .find('input[type=\'text\']')
                .val(this.value + '-' + i);
                } 
            }
        });

    return error <= 0; //return error > 0 ? false : true;
    }

但我得到了这个结果:

约翰-0-1-2,约翰-0-1-2,彼得-0-1,能源部,彼得-0-1,约翰-0-1-2

怎么了?


我做了一些修改:

function eliminateDuplicates() {
        var values = new Array();
    var $input = $('input[type=\'text\']');
    var error = 0;
    $input.each(function() {
        $(this).removeClass('double-error');
        var that = this;
        if (that.value!='') {
            values[that.value] = 0;
            $('input[type=\'text\']').each(function() {
                if (this.value == that.value) {
                    values[that.value]++;
                }
            });
        }
    });

    $input.each(function(key) {
        if (values[this.value]>1) {
            var name=this.value;
            var names = values[this.value];
                values[this.value]++;
            for (var i = 0; i < names; i++){
        $(this).parent().find('input[type=\'text\']').val(name + '-' + i);
            }


        }

    });
checkDoubles();
    return error <= 0; //return error > 0 ? false : true;
}

现在我按顺序而不是从 1 开始计数。 例如,如果我有 4 个重复的名字(彼得),我会得到:

彼得 3、彼得 4、彼得 5、彼得 6。 但是我需要 彼得 1 号、彼得 2 号、彼得 3 号、彼得 4 号。 怎么了?

【问题讨论】:

  • 我真的认为如果你能做出一个小提琴,我们可以帮助你更快

标签: javascript jquery duplicates


【解决方案1】:

我认为您正在寻找这样的东西:

function eliminateDuplicates() {

    var repeats = {};
    var error = false;

    //cache inputs
    var $inputs = $("input[type='text']");

    //loop through inputs and update repeats
    for (i = 0; i < $inputs.length; ++i) {
        //cache current element
        var cur = $inputs[i];

        //remove class
        $(cur).removeClass("double-error");

        //get text of this element
        var text = $(cur).val();

        //no text -- continue
        if (text === "") {
            continue;
            }
        //first time we've came across this value -- intialize it's counter to 1
        if ((text in repeats) === false) {
            repeats[text] = 1;
            }
        //repeat offender. Increment its counter.
        else {
            repeats[text] = repeats[text] + 1;
            }

        //update the the value for this one
        $(cur).val(text + "-" + repeats[text]);
        }

    return error; // always returns false since I'm not sure
                  // when it's supposed to return true.
    }

PS:我不明白什么时候error 应该是true,所以它总是false。不过,这应该足以让您继续前进。

PPS: Plunker here.

【讨论】:

  • 感谢您的建议,但这也不起作用。我无法适应。
  • @KęstutisBanišauskas:我不确定“我无法适应”是什么意思。请您解释/澄清一下?
  • 这意味着脚本不工作。我尝试进行一些更改修改,但根本不工作......
  • @KęstutisBanišauskas:你遇到错误了吗?可以加个html吗?发生了什么事?
  • 没有错误。我已经检查了 Free Java Script editor。并且浏览器上没有错误。调用此函数时没有任何反应。
猜你喜欢
  • 2017-11-19
  • 1970-01-01
  • 2021-12-04
  • 1970-01-01
  • 2018-01-05
  • 2021-06-03
  • 2019-03-28
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多