【问题标题】:Remove duplicate comma seperated numbers from input text value in jQuery从jQuery中的输入文本值中删除重复的逗号分隔数字
【发布时间】:2020-10-17 08:17:28
【问题描述】:

我在网页上有一个输入,其值类似于 1,7,1,4,22,58,58,1,1,4,7

<input type="text" name="hello" id="thankyouforhelping" value="" aria-invalid="false">

我尝试了多种方法来从中删除重复项,但没有一种方法有效。 我试过了:jQuery function to get all unique elements from an array?jQuery function to get all unique elements from an array?

我得到这样的值:

/* Grabbing up to date value to remove duplicates */
$upToDateValue = $('.wdrow-'+$projectrow+' .tasks-created input').val();

该值将始终是逗号分隔的数字,但如何删除所有重复的数字。如果你愿意,我怎么能把它从低到高排序?

这样最终的结果就是1,4,7,22,58

/* Removing duplicates */
$newValue = ???;

PS:我看到很多关于以前提出的问题的答案都是 javascript,但我的代码是 jQuery,我在将这些答案调整到我的 jQuery 代码方面遇到了很多困难。所以我很抱歉这个问题与其他已经可用的问题密切相关。像这样的问题:Get all unique values in a JavaScript array (remove duplicates) 我不知道如何使接受的答案适应我的代码。

感谢大家的帮助!

##Heretic_Monkey 帮了我很多忙,结果如下:

$upToDateValue = $('.wdrow-'+$projectrow+' .tasks-created input').val();
/* Removing duplicates */
var values = $upToDateValue.split(','); values = [...new Set(values)]; values.sort(); $newValue = values.join(',');
/* Setting new value to input */
$('.wdrow-'+$projectrow+' .tasks-created input').val($newValue);

【问题讨论】:

标签: javascript jquery sorting duplicates


【解决方案1】:

结合来自How to convert a comma separated string to an array?Easy way to turn JavaScript array into comma-separated list?Get all unique values in a JavaScript array (remove duplicates) 的答案,这是执行任务的一种方式:

// Get the value
var $upToDateValue = $('.wdrow-'+$projectrow+' .tasks-created input').val();
// Split into an array
var values = $upToDateValue.split(','); 
// Remove the duplicates and make a new array
values = [...new Set(values)]; 
// Sort the new array
values.sort(); 
// Create a new comma-delimited string from the array
var $newValue = values.join(',');
// Set the new value to input
$('.wdrow-'+$projectrow+' .tasks-created input').val($newValue);

所用代码参考:

【讨论】:

    【解决方案2】:

    我更新了代码,你可以复制函数并粘贴到你的代码中。

    然后使用$newValue = unique(values)在您的代码中调用它

    试试这个代码:

    function unique (value) {
    
      const valueList = value.split(",")
      const unique = valueList.filter((item, index, valueList) => {
        return index === valueList.indexOf(item);
      });
      const sorted = unique.sort(function(a, b) { return a - b; });
      
      return sorted.join(',')
    }
    
    
    /* you can use it this way */
    const value = "1,7,1,4,22,58,58,1,1,4,7"
    $newValue = unique(value)
    
    /* printing */
    console.log($newValue)

    【讨论】:

    • 感谢您的代码,但与我的代码结合起来会如何。因为我的主要问题是 jQuery 和 js 的结合。我应该把$upToDateValue$newValue放在哪里
    • 您打算将排序列表用作字符串还是数字?
    • 对于unique,您可以使用const unique = [...(new Set(numbers))];
    • 感谢您更新代码,但这比var values = $upToDateValue.split(','); values = [...new Set(values)]; $newValue = values.join(','); 更好吗?我完全被constreturn 弄糊涂了,老实说,我不知道如何处理$ newValue = unique(value) 它在哪里得到 () 之间的东西。我绝对不是说这是错误的,我非常感谢您的帮助,但答案超出了我的想象。我的错绝对不是你的!
    猜你喜欢
    • 2020-06-27
    • 2016-05-21
    • 1970-01-01
    • 2021-11-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多