Is there a way of converting a Set to a String directly (without first converting the set to an array)? I have gone through the documentation at Mozilla and I feel they may be a way of doing it. This is what I do:
let myset = new Set();
myset.add(3);
myset.add(" Wise ");
myset.add(" Men ");
let setStr = myset.toString();
let setArrStr = Array.from(myset).toString();
console.log("Set to String: " + setStr ); //"Set to String: [object Set]"
console.log("Set to Array to String: " + setArrStr); // "Set to Array to String: 3, Wise , Men "
You can convert to string directly like so:
let string = "";
let myset = new Set();
myset.add(3);
myset.add(" Wise ");
myset.add(" Men ");
myset.forEach(value => string += value);
console.log(string); // → "3 Wise Men "
Hope you got your answer.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With