JavaScript/set:修订间差异

来自康健生活
Kangkk​(留言 | 贡献)
Kangkk​(留言 | 贡献)
小 文字替换 -“{{leftrightflex|”替换为“{{Double-columns|”
标签:ReplaceText 移动版编辑 移动版网页编辑
 
(未显示同一用户的2个中间版本)
第1行: 第1行:
== 标题文字 ==
== 标题文字 ==
== 创建 ==
== 创建 ==
{{leftrightflex|<syntaxhighlight lang="javascript">
{{Double-columns|<syntaxhighlight lang="javascript">
new Set(['foo', 1, true, {}, undefined, function (){}]);</syntaxhighlight>|{{consoleout|<pre>
new Set(['foo', 1, true, {}, undefined, function (){}]);</syntaxhighlight>|{{consoleout|<pre>
Set(6) {'foo', 1, true, {…}, undefined, …}
Set(6) {'foo', 1, true, {…}, undefined, …}
第19行: 第19行:
     size: 6
     size: 6
     [[Prototype]]: Set
     [[Prototype]]: Set
</pre>}}}}
== 方法 ==
=== add ===
{{Double-columns|<syntaxhighlight lang="javascript">
const _set = new Set();
_set.add(1).add(2).add(3);
console.log(Array.from(_set));</syntaxhighlight>|{{consoleout|<pre>
[ 1, 2, 3 ]
</pre>}}}}
== 格式转换 ==
=== set 转 array ===
{{Double-columns|<syntaxhighlight lang="javascript">
const _set = new Set([1, 2, 3]);
console.log(Array.from(_set));</syntaxhighlight>|{{consoleout|<pre>
[ 1, 2, 3 ]
</pre>}}}}
</pre>}}}}

2026年11月26日 (四) 08:25的最新版本

标题文字

创建

new Set(['foo', 1, true, {}, undefined, function (){}]);
Set(6) {'foo', 1, true, {…}, undefined, …}
  [[Entries]]
    0: "foo"
      value: "foo"
    1: 1
      value: 1
    2: true
      value: true
    3: 
      value:
        [[Prototype]]: Object
    4: undefined
      value: undefined
    5: function (){}
    size: 6
    [[Prototype]]: Set

方法

add

const _set = new Set();
_set.add(1).add(2).add(3);
console.log(Array.from(_set));
[ 1, 2, 3 ]

格式转换

set 转 array

const _set = new Set([1, 2, 3]);
console.log(Array.from(_set));
[ 1, 2, 3 ]