aboutsummaryrefslogtreecommitdiff
path: root/files/pt-br/web/javascript/reference/global_objects/set/index.html
blob: f795f2f403953fe0880861db4799101160f4294c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
---
title: Set
slug: Web/JavaScript/Reference/Global_Objects/Set
tags:
  - ECMAScript 2015
  - JavaScript
  - Objetos
  - Objetos Globais
  - conjuntos
translation_of: Web/JavaScript/Reference/Global_Objects/Set
---
<div>{{JSRef("Global_Objects", "Set")}}</div>

<h2 id="Sumário">Sumário</h2>

<p>O objeto <strong><code>Set</code></strong> permite que você armazene valores <em>únicos</em> de qualquer tipo, desde {{Glossary("Primitive", "valores primitivos")}} a referências a objetos.</p>

<h2 id="Sintaxe">Sintaxe</h2>

<pre class="syntaxbox"> new Set([iterable]);</pre>

<h3 id="Parâmetros">Parâmetros</h3>

<dl>
 <dt>iterable</dt>
 <dd>Se um <a href="/en-US/docs/Web/JavaScript/Reference/Statements/for...of">objeto iterável </a>é passado, todos os seus elementos serão adicionados ao novo Set. Se tal parâmetro não for específicado, ou se seu valor for <code>null</code>, o novo <code>Set</code> estará vazio.</dd>
</dl>

<h2 id="Descrição">Descrição</h2>

<p>Objetos <code style="font-style: normal; line-height: 1.5;">Set</code><span style="line-height: 1.5;"> são coleções de valores nas quais é possível iterar os elementos em ordem de inserção. Um valor no </span><code style="font-style: normal; line-height: 1.5;">Set</code><span style="line-height: 1.5;"> <strong>pode ocorrer apenas uma vez</strong>; ele é único na coleção do </span><code style="font-style: normal; line-height: 1.5;">Set</code><span style="line-height: 1.5;">.</span></p>

<h3 id="Igualdade_de_valores">Igualdade de valores</h3>

<p>Como cada valor no Set deve ser único, a igualdade será checada e não é baseada no mesmo algoritmo que aquele usado no operador ===. Especificamente, para <code>Set</code>s, <code>+0</code> (que é exatamente igual a - 0) e <code>- 0</code> são valores diferentes. No entanto, isto foi modificado na última especificação ECMAScript 2015. Iniciado com o Gecko 29.0 {{geckoRelease("29")}} ({{bug("952870")}}) e pelo  <a href="https://code.google.com/p/v8/issues/detail?id=3069">recent nightly Chrome</a>, +0 e -0 são tratados com sendo o mesmo valor em objetos conjunto (Set). Também, <code>NaN</code> e <code>undefined </code>podem ser armazenados em um conjunto Set.</p>

<h2 id="Properties" name="Properties">Propriedades</h2>

<dl>
 <dt><code>Set.length</code></dt>
 <dd>O valor da propriedade comprimento é 0.</dd>
 <dd>Para contar o número de elementos de um <code>Set</code>, use {{jsxref("Set.prototype.size")}}.</dd>
 <dt>{{jsxref("Set.@@species", "get Set[@@species]")}}</dt>
 <dd>A função construtora usada para criar objetos derivados.</dd>
 <dt>{{jsxref("Set.prototype")}}</dt>
 <dd>Representa o prototype do constructor do <code>Set</code>. Permite a adição de propriedades para todos os objetos do tipo <code>Set</code>.</dd>
</dl>

<h2 id="Boolean_instances" name="Boolean_instances">Instâncias <code>Set</code> </h2>

<p>Todas as instâncias de <code>Set</code> herdam de {{jsxref("Set.prototype")}}.</p>

<h3 id="Propriedades">Propriedades</h3>

<p>{{page('pt-BR/Web/JavaScript/Reference/Global_Objects/Set/prototype','Properties')}}</p>

<h3 id="Métodos">Métodos</h3>

<p>{{page('pt-BR/Web/JavaScript/Reference/Global_Objects/Set/prototype','Methods')}}</p>

<h2 id="Exemplos">Exemplos</h2>

<h3 id="Utilizando_o_objeto_Set">Utilizando o objeto <code>Set</code></h3>

<pre class="brush: js">var meuSet = new Set();

meuSet.add(1); // meuSet [1]
meuSet.add(5); // meuSet [1, 5]
meuSet.add(5); // 5 já foi adicionando, portanto, meuSet [1, 5]
meuSet.add("texto");
var o = {a: 1, b: 2};
meuSet.add(o);

meuSet.add({a: 1, b: 2}); //  o está referenciando outro objeto

meuSet.has(1); // true
meuSet.has(3); // false, 3 não foi adicionado ao set (Conjunto)
meuSet.has(5);              // true
meuSet.has(Math.sqrt(25));  // true
meuSet.has("Texto".toLowerCase()); // true
meuSet.has(o); // true

meuSet.size; // 5

meuSet.delete(5); // remove 5 do set
meuSet.has(5);    // false, 5 já foi removido

meuSet.size; // 4, nós simplesmente removemos um valor

console.log(meuSet) // Set { 1, 'texto', { a: 1, b: 2 }, { a: 1, b: 2 } }
</pre>



<h3 id="Iterando_objetos_Set">Iterando objetos <code>Set</code></h3>

<pre class="brush: js">// iterar sobre os itens em set
// loga os itens na ordem: 1, "texto"
for (let item of meuSet) console.log(item);

// loga os itens na ordem: 1, "texto"
for (let item of meuSet.keys()) console.log(item);

// loga os itens na ordem: 1, "texto"
for (let item of meuSet.values()) console.log(item);

// loga os itens na ordem: 1, "texto"
//(key e value são os mesmos aqui)
for (let [key, value] of meuSet.entries()) console.log(key);

// converte Set para um Array
var meuArr = [v for (v of mySet)]; // [1, "some text"]

// o seguinte também funcionará se for executado em um documento HTML
mySet.add(document.body);
mySet.has(document.querySelector("body")); // true

// a conversão entre Set e Array
mySet2 = Set([1,2,3,4]);
mySet2.size; // 4
[...mySet2]; // [1,2,3,4]

// intersecção pode ser simulado via
var intersection = new Set([...set1].filter(x =&gt; set2.has(x)));

// Iterar entradas set com forEach
meuSet.forEach(function(value) {
  console.log(value);
});

// 1
// 2
// 3
// 4</pre>

<h3 id="Implementando_operações_básicas_entre_conjuntos">Implementando operações básicas entre conjuntos</h3>

<pre class="brush: js line-numbers  language-js"><code class="language-js"><span class="keyword token">function</span> is<span class="function token">Superset</span><span class="punctuation token">(</span><span class="keyword token">set</span><span class="punctuation token">,</span> subset<span class="punctuation token">)</span> <span class="punctuation token">{</span>
    <span class="keyword token">for</span> <span class="punctuation token">(</span><span class="keyword token">var</span> elem <span class="keyword token">of</span> subset<span class="punctuation token">)</span> <span class="punctuation token">{</span>
        <span class="keyword token">if</span> <span class="punctuation token">(</span><span class="operator token">!</span><span class="keyword token">set</span><span class="punctuation token">.</span><span class="function token">has</span><span class="punctuation token">(</span>elem<span class="punctuation token">)</span><span class="punctuation token">)</span> <span class="punctuation token">{</span>
            <span class="keyword token">return</span> <span class="keyword token">false</span><span class="punctuation token">;</span>
        <span class="punctuation token">}</span>
    <span class="punctuation token">}</span>
    <span class="keyword token">return</span> <span class="keyword token">true</span><span class="punctuation token">;</span>
<span class="punctuation token">}</span>

<span class="keyword token">function</span> <span class="function token">uniao</span><span class="punctuation token">(</span>setA<span class="punctuation token">,</span> setB<span class="punctuation token">)</span> <span class="punctuation token">{</span>
    <span class="keyword token">var</span> _uniao <span class="operator token">=</span> <span class="keyword token">new</span> <span class="class-name token">Set</span><span class="punctuation token">(</span>setA<span class="punctuation token">)</span><span class="punctuation token">;</span>
    <span class="keyword token">for</span> <span class="punctuation token">(</span><span class="keyword token">var</span> elem <span class="keyword token">of</span> setB<span class="punctuation token">)</span> <span class="punctuation token">{</span>
        _uniao<span class="punctuation token">.</span><span class="function token">add</span><span class="punctuation token">(</span>elem<span class="punctuation token">)</span><span class="punctuation token">;</span>
    <span class="punctuation token">}</span>
    <span class="keyword token">return</span> _uniao<span class="punctuation token">;</span>
<span class="punctuation token">}</span>

<span class="keyword token">function</span> <span class="function token">interseccao</span><span class="punctuation token">(</span>setA<span class="punctuation token">,</span> setB<span class="punctuation token">)</span> <span class="punctuation token">{</span>
    <span class="keyword token">var</span> _<span class="function token">interseccao</span> <span class="operator token">=</span> <span class="keyword token">new</span> <span class="class-name token">Set</span><span class="punctuation token">(</span><span class="punctuation token">)</span><span class="punctuation token">;</span>
    <span class="keyword token">for</span> <span class="punctuation token">(</span><span class="keyword token">var</span> elem <span class="keyword token">of</span> setB<span class="punctuation token">)</span> <span class="punctuation token">{</span>
        <span class="keyword token">if</span> <span class="punctuation token">(</span>setA<span class="punctuation token">.</span><span class="function token">has</span><span class="punctuation token">(</span>elem<span class="punctuation token">)</span><span class="punctuation token">)</span> <span class="punctuation token">{</span>
            _<span class="function token">interseccao</span><span class="punctuation token">.</span><span class="function token">add</span><span class="punctuation token">(</span>elem<span class="punctuation token">)</span><span class="punctuation token">;</span>
        <span class="punctuation token">}</span>
    <span class="punctuation token">}</span>
    <span class="keyword token">return</span> _<span class="function token">interseccao</span><span class="punctuation token">;</span>
<span class="punctuation token">}</span>

<span class="keyword token">function</span> <span class="function token">diferencaSimetrica</span><span class="punctuation token">(</span>setA<span class="punctuation token">,</span> setB<span class="punctuation token">)</span> <span class="punctuation token">{</span>
    <span class="keyword token">var</span> _diferenca <span class="operator token">=</span> <span class="keyword token">new</span> <span class="class-name token">Set</span><span class="punctuation token">(</span>setA<span class="punctuation token">)</span><span class="punctuation token">;</span>
    <span class="keyword token">for</span> <span class="punctuation token">(</span><span class="keyword token">var</span> elem <span class="keyword token">of</span> setB<span class="punctuation token">)</span> <span class="punctuation token">{</span>
        <span class="keyword token">if</span> <span class="punctuation token">(</span>_diferenca<span class="punctuation token">.</span><span class="function token">has</span><span class="punctuation token">(</span>elem<span class="punctuation token">)</span><span class="punctuation token">)</span> <span class="punctuation token">{</span>
            _diferenca<span class="punctuation token">.</span><span class="keyword token">delete</span><span class="punctuation token">(</span>elem<span class="punctuation token">)</span><span class="punctuation token">;</span>
        <span class="punctuation token">}</span> <span class="keyword token">else</span> <span class="punctuation token">{</span>
            _diferenca<span class="punctuation token">.</span><span class="function token">add</span><span class="punctuation token">(</span>elem<span class="punctuation token">)</span><span class="punctuation token">;</span>
        <span class="punctuation token">}</span>
    <span class="punctuation token">}</span>
    <span class="keyword token">return</span> _diferenca<span class="punctuation token">;</span>
<span class="punctuation token">}</span>

<span class="keyword token">function</span> <span class="function token">diferenca</span><span class="punctuation token">(</span>setA<span class="punctuation token">,</span> setB<span class="punctuation token">)</span> <span class="punctuation token">{</span>
    <span class="keyword token">var</span> _diferenca <span class="operator token">=</span> <span class="keyword token">new</span> <span class="class-name token">Set</span><span class="punctuation token">(</span>setA<span class="punctuation token">)</span><span class="punctuation token">;</span>
    <span class="keyword token">for</span> <span class="punctuation token">(</span><span class="keyword token">var</span> elem <span class="keyword token">of</span> setB<span class="punctuation token">)</span> <span class="punctuation token">{</span>
        _diferenca<span class="punctuation token">.</span><span class="keyword token">delete</span><span class="punctuation token">(</span>elem<span class="punctuation token">)</span><span class="punctuation token">;</span>
    <span class="punctuation token">}</span>
    <span class="keyword token">return</span> _diferenca<span class="punctuation token">;</span>
<span class="punctuation token">}</span>

<span class="comment token">//Exemplos</span>
<span class="keyword token">var</span> setA <span class="operator token">=</span> <span class="keyword token">new</span> <span class="class-name token">Set</span><span class="punctuation token">(</span><span class="punctuation token">[</span><span class="number token">1</span><span class="punctuation token">,</span> <span class="number token">2</span><span class="punctuation token">,</span> <span class="number token">3</span><span class="punctuation token">,</span> <span class="number token">4</span><span class="punctuation token">]</span><span class="punctuation token">)</span><span class="punctuation token">,</span>
    setB <span class="operator token">=</span> <span class="keyword token">new</span> <span class="class-name token">Set</span><span class="punctuation token">(</span><span class="punctuation token">[</span><span class="number token">2</span><span class="punctuation token">,</span> <span class="number token">3</span><span class="punctuation token">]</span><span class="punctuation token">)</span><span class="punctuation token">,</span>
    setC <span class="operator token">=</span> <span class="keyword token">new</span> <span class="class-name token">Set</span><span class="punctuation token">(</span><span class="punctuation token">[</span><span class="number token">3</span><span class="punctuation token">,</span> <span class="number token">4</span><span class="punctuation token">,</span> <span class="number token">5</span><span class="punctuation token">,</span> <span class="number token">6</span><span class="punctuation token">]</span><span class="punctuation token">)</span><span class="punctuation token">;</span>

<span class="function token">isSuperset</span><span class="punctuation token">(</span>setA<span class="punctuation token">,</span> setB<span class="punctuation token">)</span><span class="punctuation token">;</span> <span class="comment token">// =&gt; true</span>
<span class="function token">uniao</span><span class="punctuation token">(</span>setA<span class="punctuation token">,</span> setC<span class="punctuation token">)</span><span class="punctuation token">;</span> <span class="comment token">// =&gt; Set [1, 2, 3, 4, 5, 6]</span>
<span class="function token">interseccao</span><span class="punctuation token">(</span>setA<span class="punctuation token">,</span> setC<span class="punctuation token">)</span><span class="punctuation token">;</span> <span class="comment token">// =&gt; Set [3, 4]</span>
<span class="function token">diferencaSimetrica</span><span class="punctuation token">(</span>setA<span class="punctuation token">,</span> setC<span class="punctuation token">)</span><span class="punctuation token">;</span> <span class="comment token">// =&gt; Set [1, 2, 5, 6]</span>
<span class="function token">diferenca</span><span class="punctuation token">(</span>setA<span class="punctuation token">,</span> setC<span class="punctuation token">)</span><span class="punctuation token">;</span> <span class="comment token">// =&gt; Set [1, 2]</span></code></pre>

<h3 id="Relação_com_objetos_Array">Relação com objetos <code>Array</code> </h3>

<pre class="brush: js">var myArray = ["value1", "value2", "value3"];

// Use o construtor regular de Set para transformar um array dentro de um Set
var mySet = new Set(myArray);

mySet.has("value1"); // retorna true

// Use o operador de propagação para transformar um Set em um Array.
alert(uneval([...mySet])); // Irá mostrar-lhe exatamente o mesmo Array como myArray</pre>

<h3 id="Removendo_elementos_duplicados_de_um_Array">Removendo elementos duplicados de um <code>Array</code></h3>

<pre class="line-numbers  language-html"><code class="language-html">// Use para remover elementos duplicados de um Array

const numeros = [2,3,4,4,2,3,3,4,4,5,5,6,6,7,5,32,3,4,5]

console.log([...new Set(numeros)])

// [2, 3, 4, 5, 6, 7, 32]</code></pre>

<h3 id="Relação_com_objetos_String">Relação com objetos <code>String</code></h3>

<pre class="brush: js line-numbers  language-js"><code class="language-js"><span class="keyword token">var</span> texto <span class="operator token">=</span> <span class="string token">'India'</span><span class="punctuation token">;</span>

<span class="keyword token">var</span> meuSet <span class="operator token">=</span> <span class="keyword token">new</span> <span class="class-name token">Set</span><span class="punctuation token">(</span>texto<span class="punctuation token">)</span><span class="punctuation token">;</span>  <span class="comment token">// Set ['I', 'n', 'd', 'i', 'a']</span>
meuSet<span class="punctuation token">.</span>size<span class="punctuation token">;</span>  <span class="comment token">// 5</span></code></pre>

<h2 id="Especificações">Especificações</h2>

<table class="standard-table">
 <tbody>
  <tr>
   <th scope="col">Especificação</th>
   <th scope="col">Status</th>
   <th scope="col">Comentário</th>
  </tr>
  <tr>
   <td>{{SpecName('ES2015', '#sec-set-objects', 'Set')}}</td>
   <td>{{Spec2('ES2015')}}</td>
   <td>Definição inicial.</td>
  </tr>
  <tr>
   <td>{{SpecName('ESDraft', '#sec-set-objects', 'Set')}}</td>
   <td>{{Spec2('ESDraft')}}</td>
   <td></td>
  </tr>
 </tbody>
</table>

<h2 id="Browser_compatibility">Compatibilidade com navegadores</h2>



<p>{{Compat("javascript.builtins.Set")}}</p>

<h2 id="Veja_também">Veja também</h2>

<ul>
 <li>{{jsxref("Map")}}</li>
 <li>{{jsxref("WeakMap")}}</li>
 <li>{{jsxref("WeakSet")}}</li>
</ul>