为什么 Set 对象拆分字符串?
Why is Set object split string?
const set = new Set("123");
console.log(set);
输出是“设置{'1','2','3'}”
因为字符串是 iterable, and when you give the Set
constructor an iterable object,¹ it loops through the values provided by the object's iterator and adds them to the set. In the case of strings, the string iterator iterates the strings code points — in your example, "1"
, "2"
, and "3"
. (For more about "code points" vs. "code units" and what a string is, read my blog post。)
如果要将字符串添加到集合中,则必须单独添加或将其包装在另一个可迭代对象(如数组)中:
// Add separately:
let set = new Set();
set.add("123");
console.log(set.size); // 1
console.log([...set]); // ["123"]
// Or wrap:
set = new Set(["123"]);
console.log(set.size); // 1
console.log([...set]); // ["123"]
¹(或原始类型,例如强制转换为可迭代对象的字符串)
Set
constructor takes an iterable 并获取它的每个值。
字符串 have an @@iterator
property by default 使它们可迭代:
const foo = "xyz";
console.log(...foo);
const [a, b, c] = foo;
console.log(a, b, c);
for (const char of foo)
console.log(char);
因此,这正是将字符串传递给 Set
构造函数时发生的情况 - 它绘制每个值,就像处理任何其他可迭代对象一样:
const foo = "xyz";
const bar = [1, 2, 3]
console.log("set of string:", new Set(foo));
console.log("set of array :", new Set(bar));
<h1>Check the browser console</h1>
const set = new Set("123");
console.log(set);
输出是“设置{'1','2','3'}”
因为字符串是 iterable, and when you give the Set
constructor an iterable object,¹ it loops through the values provided by the object's iterator and adds them to the set. In the case of strings, the string iterator iterates the strings code points — in your example, "1"
, "2"
, and "3"
. (For more about "code points" vs. "code units" and what a string is, read my blog post。)
如果要将字符串添加到集合中,则必须单独添加或将其包装在另一个可迭代对象(如数组)中:
// Add separately:
let set = new Set();
set.add("123");
console.log(set.size); // 1
console.log([...set]); // ["123"]
// Or wrap:
set = new Set(["123"]);
console.log(set.size); // 1
console.log([...set]); // ["123"]
¹(或原始类型,例如强制转换为可迭代对象的字符串)
Set
constructor takes an iterable 并获取它的每个值。
字符串 have an @@iterator
property by default 使它们可迭代:
const foo = "xyz";
console.log(...foo);
const [a, b, c] = foo;
console.log(a, b, c);
for (const char of foo)
console.log(char);
因此,这正是将字符串传递给 Set
构造函数时发生的情况 - 它绘制每个值,就像处理任何其他可迭代对象一样:
const foo = "xyz";
const bar = [1, 2, 3]
console.log("set of string:", new Set(foo));
console.log("set of array :", new Set(bar));
<h1>Check the browser console</h1>