Sunday, May 19, 2024
 Popular · Latest · Hot · Upcoming
3
rated 0 times [  7] [ 4]  / answers: 1 / hits: 63406  / 7 Years ago, sat, november 11, 2017, 12:00:00

I want to convert JavaScript Set to string with space.



For example, if I have a set like:



var foo = new Set();
foo.add('hello');
foo.add('world');
foo.add('JavaScript');


And I'd like to print the string from the set: hello world JavaScript (space between each element).



I tried below codes but they are not working:



foo.toString(); // Not working
String(foo); // Not working


Is there simplest and easiest way to convert from Set to string?


More From » string

 Answers
19

You can use Array.from:



Array.from(foo).join(' ')


or the spread syntax:



[...foo].join(' ')

[#55962] Tuesday, November 7, 2017, 7 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
brennanm

Total Points: 510
Total Questions: 103
Total Answers: 95

Location: Nicaragua
Member since Tue, Dec 8, 2020
4 Years ago
;