有没有办法直接将Set转换为String(没有先将该转换为数组)?我已经阅读了Mozilla的文档,我觉得它们可能是一种方法。这就是我的工作:
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 "
答案 0 :(得分:0)
您可以直接转换为字符串,如下所示:
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 "

希望你得到答案。