JavaScript/set:修订间差异
来自康健生活
小 文字替换 -“{{leftrightflex|”替换为“{{Double-columns|” 标签:ReplaceText 移动版编辑 移动版网页编辑 |
|||
| (未显示同一用户的1个中间版本) | |||
| 第1行: | 第1行: | ||
== 标题文字 == | == 标题文字 == | ||
== 创建 == | == 创建 == | ||
{{ | {{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>}}}} | </pre>}}}} | ||
== 格式转换 == | == 格式转换 == | ||
=== set 转 array === | === set 转 array === | ||
{{ | {{Double-columns|<syntaxhighlight lang="javascript"> | ||
const _set = new Set([1, 2, 3]); | const _set = new Set([1, 2, 3]); | ||
console.log(Array.from(_set));</syntaxhighlight>|{{consoleout|<pre> | console.log(Array.from(_set));</syntaxhighlight>|{{consoleout|<pre> | ||
[ 1, 2, 3 ] | [ 1, 2, 3 ] | ||
</pre>}}}} | </pre>}}}} | ||