Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Remove duplicate in a string - javascript

Tags:

javascript

I have a string in javascript where there are a lot of duplicates. For example I have:

var x = "Int32,Int32,Int32,Int32,Int32,Int32,Int32,Int32,Int32,Double,Double,Double"

What can I do to delete duplicates and to get for example x="Int32,Double"?

like image 441
DistribuzioneGaussiana Avatar asked Feb 24 '16 18:02

DistribuzioneGaussiana


People also ask

What is the best way to remove duplicates from a Javascript array?

Use the filter() method: The filter() method creates a new array of elements that pass the condition we provide. It will include only those elements for which true is returned. We can remove duplicate values from the array by simply adjusting our condition.

Does Set remove duplicates Javascript?

1) Remove duplicates from an array using a SetThe new Set will implicitly remove duplicate elements. Then, convert the set back to an array.


1 Answers

With Set and Array.from this is pretty easy:

Array.from(new Set(x.split(','))).toString()

var x = "Int32,Int32,Int32,Int32,Int32,Int32,Int32,Int32,Int32,Double,Double,Double"
x = Array.from(new Set(x.split(','))).toString();
document.write(x);
like image 108
MinusFour Avatar answered Oct 05 '22 20:10

MinusFour