Files
freeCodeCamp/curriculum/challenges/ukrainian/02-javascript-algorithms-and-data-structures/es6/use-destructuring-assignment-to-assign-variables-from-objects.md

2.9 KiB
Raw Permalink Blame History

id, title, challengeType, forumTopicId, dashedName
id title challengeType forumTopicId dashedName
587d7b89367417b2b2512b49 Використовуйте деструктивне привласнення для визначення змінних із масивів 1 301215 use-destructuring-assignment-to-assign-variables-from-objects

--description--

Деструктуризація дозволяє вам призначити нове ім'я змінної при вилученні. Щоб зробити це, поставте нове ім'я після двокрапки при призначенні.

Використовуйте то й же об'єкт що і в останньому прикладі:

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

Розглянемо, як ви можете дати нові імена змінним у призначенні:

const { name: userName, age: userAge } = user;

Ви можете прочитати це як "отримати значення user.name та та назначити нову змінну з назвою userName" і т.д. Значення userName буде John Doe, і значення userAge буде числом 34.

--instructions--

Замініть два визначення еквівалентами з деструктивного привласнення. Ви все ще призначаєте змінним highToday та highTomorrow значення today та and tomorrow з об'єкту HIGH_TEMPERATURES.

--hints--

Видаліть призначення синтаксису ES5.

assert(
  !code.match(/highToday = HIGH_TEMPERATURES\.today/g) &&
    !code.match(/highTomorrow = HIGH_TEMPERATURES\.tomorrow/g)
);

Використовуйте деструкцію, щоб створити змінну highToday.

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

Використовуйте деструкцію, щоб створити змінну highTomorrow.

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

highToday повинна дорівнювати 77 та highTomorrow має бути рівний 80.

assert(highToday === 77 && highTomorrow === 80);

--seed--

--seed-contents--

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

// Only change code below this line

const highToday = HIGH_TEMPERATURES.today;
const highTomorrow = HIGH_TEMPERATURES.tomorrow; 

// Only change code above this line

--solutions--

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

const { today: highToday, tomorrow: highTomorrow } = HIGH_TEMPERATURES;