2021-02-06 04:42:36 +00:00
---
id: 587d7b89367417b2b2512b49
2021-03-18 11:16:46 -06:00
title: Usa sintaxis de desestructuración para asignar variables desde objetos
2021-02-06 04:42:36 +00:00
challengeType: 1
forumTopicId: 301215
dashedName: use-destructuring-assignment-to-assign-variables-from-objects
---
# --description--
2021-03-18 11:16:46 -06:00
La desestructuración te permite asignar un nuevo nombre de variable al extraer valores. Puedes hacer esto al poner el nuevo nombre después de dos puntos al asignar el valor.
2021-02-06 04:42:36 +00:00
2021-03-18 11:16:46 -06:00
Usando el mismo objeto del ejemplo anterior:
2021-02-06 04:42:36 +00:00
```js
const user = { name: 'John Doe', age: 34 };
```
2021-03-18 11:16:46 -06:00
Así es como puedes dar nuevos nombres de variables en la asignación:
2021-02-06 04:42:36 +00:00
```js
const { name: userName, age: userAge } = user;
```
2021-03-18 11:16:46 -06:00
Puedes leerlo como "obtén el valor de `user.name` y asígnalo a una nueva variable llamada `userName` " y así sucesivamente. El valor de `userName` sería la cadena `John Doe` , y el valor de `userAge` sería el número `34` .
2021-02-06 04:42:36 +00:00
# --instructions--
2021-03-18 11:16:46 -06:00
Reemplaza las dos asignaciones con una sintaxis de desestructuración equivalente. Todavía deben seguir asignando las variables `highToday` y `highTomorrow` con los valores de `today` y `tomorrow` del objeto `HIGH_TEMPERATURES` .
2021-02-06 04:42:36 +00:00
# --hints--
2021-03-18 11:16:46 -06:00
Debes eliminar la sintaxis de asignación ES5.
2021-02-06 04:42:36 +00:00
```js
assert(
!code.match(/highToday = HIGH_TEMPERATURES\.today/g) &&
!code.match(/highTomorrow = HIGH_TEMPERATURES\.tomorrow/g)
);
```
2021-03-18 11:16:46 -06:00
Debes usar desestructuración para crear la variable `highToday` .
2021-02-06 04:42:36 +00:00
```js
assert(
code.match(
/(var|const|let)\s*{\s*(today\s*:\s*highToday[^}]*|[^,]*,\s*today\s*:\s*highToday\s*)}\s*=\s*HIGH_TEMPERATURES(;|\s+|\/\/)/g
)
);
```
2021-03-18 11:16:46 -06:00
Debes usar desestructuración para crear la variable `highTomorrow` .
2021-02-06 04:42:36 +00:00
```js
assert(
code.match(
/(var|const|let)\s*{\s*(tomorrow\s*:\s*highTomorrow[^}]*|[^,]*,\s*tomorrow\s*:\s*highTomorrow\s*)}\s*=\s*HIGH_TEMPERATURES(;|\s+|\/\/)/g
)
);
```
2021-03-18 11:16:46 -06:00
`highToday` debe ser igual a `77` y `highTomorrow` debe ser igual a `80` .
2021-02-06 04:42:36 +00:00
```js
assert(highToday === 77 & & highTomorrow === 80);
```
# --seed--
## --seed-contents--
```js
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--
```js
const HIGH_TEMPERATURES = {
yesterday: 75,
today: 77,
tomorrow: 80
};
const { today: highToday, tomorrow: highTomorrow } = HIGH_TEMPERATURES;
```