purple11111
purple11111

Reputation: 719

Remove duplicate comma seperated numbers from input text value in jQuery

I have an input on a webpage with a value like this 1,7,1,4,22,58,58,1,1,4,7

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

I tried numerous ways to remove the duplicate from it but none of the ways work. I tried: jQuery function to get all unique elements from an array? and jQuery function to get all unique elements from an array?

I get the value like this:

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

The value will always be numbers that are comma seperated but how can I remove all the duplicated numbers. And if you are willing how could I also sort it from low to high?

So that the end result would be 1,4,7,22,58

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

PS: I see a lot of answers on previously asked questions are javascript but I have my code in jQuery and I am struggling a lot with adapting those answers to my jQuery code. So I am sorry that this question is so closely related to others already available. Questions like this: Get all unique values in a JavaScript array (remove duplicates) I have no clue how to adapt the accepted answer to my code.

Thanks everyone for helping!

##Heretic_Monkey helped me out a lot and this is the result:

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

Upvotes: 0

Views: 1027

Answers (2)

Heretic Monkey
Heretic Monkey

Reputation: 12114

Combining answers from How to convert a comma separated string to an array?, Easy way to turn JavaScript array into comma-separated list?, and Get all unique values in a JavaScript array (remove duplicates), this is one way of performing the task:

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

References for code used:

Upvotes: 1

Marik Ishtar
Marik Ishtar

Reputation: 3049

I updated the code, you can copy the function and paste in your code.

then call it in your code using $newValue = unique(values)

Try this code:

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)

Upvotes: 0

Related Questions