2021-06-15 07:49:18 +00:00
---
id: 587d8254367417b2b2512c70
2021-10-27 15:10:57 +00:00
title: Creare e aggiungere ai set in ES6
2021-06-15 07:49:18 +00:00
challengeType: 1
forumTopicId: 301636
dashedName: create-and-add-to-sets-in-es6
---
# --description--
2021-10-27 15:10:57 +00:00
Ora che avete lavorato su ES5, state per eseguire qualcosa di simile in ES6. Questo sarà molto più facile. ES6 contiene una struttura di dati integrata `Set` quindi molte delle operazioni che hai scritto a mano sono ora incluse per te. Diamo un'occhiata:
2021-06-15 07:49:18 +00:00
2021-10-27 15:10:57 +00:00
Per creare un nuovo set vuoto:
2021-06-15 07:49:18 +00:00
```js
var set = new Set();
```
2021-10-27 15:10:57 +00:00
È possibile creare un set con un valore:
2021-06-15 07:49:18 +00:00
```js
var set = new Set(1);
```
2021-10-27 15:10:57 +00:00
È possibile creare un set con un array:
2021-06-15 07:49:18 +00:00
```js
var set = new Set([1, 2, 3]);
```
2021-10-27 15:10:57 +00:00
Una volta creato un set, puoi aggiungere i valori che desideri utilizzando il metodo `add` :
2021-06-15 07:49:18 +00:00
```js
var set = new Set([1, 2, 3]);
set.add([4, 5, 6]);
```
2021-10-27 15:10:57 +00:00
Come promemoria, un insieme è una struttura di dati che non può contenere valori duplicati:
2021-06-15 07:49:18 +00:00
```js
var set = new Set([1, 2, 3, 1, 2, 3]);
// set contains [1, 2, 3] only
```
# --instructions--
2021-10-27 15:10:57 +00:00
Per questo esercizio, restituisci un set con i seguenti valori: `1, 2, 3, 'Taco', 'Cat', 'Awesome'`
2021-06-15 07:49:18 +00:00
# --hints--
2021-10-27 15:10:57 +00:00
Il tuo `Set` deve contenere solo i valori `1, 2, 3, Taco, Cat, Awesome` .
2021-06-15 07:49:18 +00:00
```js
assert(
(function () {
var test = checkSet();
return (
test.size == 6 & &
test.has(1) & &
test.has(2) & &
test.has(3) & &
test.has('Taco') & &
test.has('Cat') & &
test.has('Awesome')
);
})()
);
```
# --seed--
## --seed-contents--
```js
function checkSet() {
var set = new Set([1, 2, 3, 3, 2, 1, 2, 3, 1]);
// Only change code below this line
// Only change code above this line
console.log(Array.from(set));
return set;
}
checkSet();
```
# --solutions--
```js
function checkSet(){var set = new Set([1,2,3,'Taco','Cat','Awesome']);
return set;}
```