Publicado em: 03/Dec/2019
Atualizado em: 06/Dec/2019

javascript set

https://alligator.io/js/sets-introduction/

 1let animals = new Set();
 2
 3animals.add('🐷');
 4animals.add('🐼');
 5animals.add('🐢');
 6animals.add('🐿');
 7console.log(animals.size); // 4
 8animals.add('🐼');
 9console.log(animals.size); // 4
10
11console.log(animals.has('🐷')); // true
12animals.delete('🐷');
13console.log(animals.has('🐷')); // false
14
15animals.forEach(animal => {
16  console.log(\`Hey ${animal}!\`);
17});
18
19// Hey 🐼!
20// Hey 🐢!
21// Hey 🐿!
22
23animals.clear();
24console.log(animals.size); // 0
 1let myAnimals = new Set(['🐷', '🐢', '🐷', '🐷']);
 2
 3myAnimals.add(['🐨', '🐑']);
 4myAnimals.add({ name: 'Rud', type: '🐢' });
 5console.log(myAnimals.size); // 4
 6
 7myAnimals.forEach(animal => {
 8  console.log( animal );
 9});
10
11
12// 🐷
13// 🐢
14// ["🐨", "🐑"]
15// Object { name: "Rud", type: "🐢" }
comments powered by Disqus