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