freeCodeCamp/curriculum/challenges/italian/02-javascript-algorithms-an.../es6/use-destructuring-assignmen...

2.4 KiB

id title challengeType forumTopicId dashedName
5cfa550e84205a357704ccb6 Usare l'assegnazione destrutturante per estrarre valori dagli oggetti 1 301216 use-destructuring-assignment-to-extract-values-from-objects

--description--

L'assegnazione destrutturante è una sintassi speciale introdotta in ES6, per assegnare efficacemente dei valori presi da un oggetto.

Considerare il seguente codice ES5:

const user = { name: 'John Doe', age: 34 };

const name = user.name;
const age = user.age;

name avrebbe come valore la stringa John Doe, e age avrebbe il numero 34.

Ecco una dichiarazione di assegnazione equivalente che utilizza la sintassi di destrutturazione ES6:

const { name, age } = user;

Ancora una volta, name avrà come valore la stringa John Doe, e age il numero 34.

Qui, le variabili name e age verranno create e assegnate ai rispettivi valori nell'oggetto user. Puoi constatare quanto questo sia più pulito.

Potrai estrarre dall'oggetto tutti i valori che desideri.

--instructions--

Sostituisci le due assegnazioni con un'assegnazione destrutturante equivalente. Dovrebbe ancora assegnare alle variabili today e tomorrow i valori di today e tomorrow dell'oggetto HIGH_TEMPERATURES.

--hints--

Dovresti rimuovere la sintassi di assegnazione ES5.

assert(
  !code.match(/today\s*=\s*HIGH_TEMPERATURES\.(today|tomorrow)/g)
);

Dovresti usare la destrutturazione per creare la variabile today.

assert(
  code.match(/(var|let|const)\s*{\s*(today[^}]*|[^,]*,\s*today)\s*}\s*=\s*HIGH_TEMPERATURES(;|\s+|\/\/)/g)
);

Dovresti usare la destrutturazione per creare la variabile tomorrow.

assert(
  code.match(/(var|let|const)\s*{\s*(tomorrow[^}]*|[^,]*,\s*tomorrow)\s*}\s*=\s*HIGH_TEMPERATURES(;|\s+|\/\/)/g)
);

today dovrebbe essere uguale a 77 e tomorrow dovrebbe essere uguale a 80.

assert(today === 77 && tomorrow === 80);

--seed--

--seed-contents--

const HIGH_TEMPERATURES = {
  yesterday: 75,
  today: 77,
  tomorrow: 80
};

// Only change code below this line

const today = HIGH_TEMPERATURES.today;
const tomorrow = HIGH_TEMPERATURES.tomorrow;

// Only change code above this line

--solutions--

const HIGH_TEMPERATURES = {
  yesterday: 75,
  today: 77,
  tomorrow: 80
};

const { today, tomorrow } = HIGH_TEMPERATURES;