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

65 lines
2.7 KiB
Markdown
Raw Normal View History

2018-10-08 17:34:43 +00:00
---
id: 587d7b89367417b2b2512b49
title: Use Destructuring Assignment to Assign Variables from Objects
challengeType: 1
2018-10-10 20:20:40 +00:00
videoUrl: ''
localeTitle: Usar asignación de destrucción para asignar variables de objetos
2018-10-08 17:34:43 +00:00
---
## Description
2018-10-10 20:20:40 +00:00
<section id="description"> Anteriormente vimos cómo el operador de propagación puede difundir o desempaquetar efectivamente el contenido de la matriz. Podemos hacer algo similar con los objetos también. <dfn>La asignación de destrucción</dfn> es una sintaxis especial para asignar cuidadosamente valores tomados directamente de un objeto a variables. Considere el siguiente código ES5: <blockquote> var voxel = {x: 3.6, y: 7.4, z: 6.54}; <br> var x = voxel.x; // x = 3.6 <br> var y = voxel.y; // y = 7.4 <br> var z = voxel.z; // z = 6.54 </blockquote> Aquí está la misma declaración de asignación con la sintaxis de desestructuración ES6: <blockquote> const {x, y, z} = voxel; // x = 3.6, y = 7.4, z = 6.54 </blockquote> Si, por el contrario, desea almacenar los valores de <code>voxel.x</code> en <code>a</code> , <code>voxel.y</code> en <code>b</code> , y <code>voxel.z</code> en <code>c</code> , también tiene esa libertad. <blockquote> const {x: a, y: b, z: c} = vóxel // a = 3.6, b = 7.4, c = 6.54 </blockquote> Puede leerlo como &quot;obtener el campo <code>x</code> y copiar el valor en <code>a</code> &quot;, y así sucesivamente. </section>
2018-10-08 17:34:43 +00:00
## Instructions
2018-10-10 20:20:40 +00:00
<section id="instructions"> Utilice la desestructuración para obtener la temperatura promedio para mañana desde el objeto de entrada <code>AVG_TEMPERATURES</code> , y asigne un valor con la clave <code>tomorrow</code> a <code>tempOfTomorrow</code> en línea. </section>
2018-10-08 17:34:43 +00:00
## Tests
<section id='tests'>
```yml
tests:
- text: <code>getTempOfTmrw(AVG_TEMPERATURES)</code> debe ser <code>79</code>
testString: 'assert(getTempOfTmrw(AVG_TEMPERATURES) === 79, "<code>getTempOfTmrw(AVG_TEMPERATURES)</code> should be <code>79</code>");'
- text: Se utilizó desestructuración con reasignación.
testString: 'getUserInput => assert(getUserInput("index").match(/\{\s*tomorrow\s*:\s*tempOfTomorrow\s*}\s*=\s*avgTemperatures/g),"destructuring with reassignment was used");'
```
</section>
## Challenge Seed
<section id='challengeSeed'>
<div id='js-seed'>
```js
const AVG_TEMPERATURES = {
today: 77.5,
tomorrow: 79
};
function getTempOfTmrw(avgTemperatures) {
"use strict";
// change code below this line
const tempOfTomorrow = undefined; // change this line
// change code above this line
return tempOfTomorrow;
}
console.log(getTempOfTmrw(AVG_TEMPERATURES)); // should be 79
2018-10-10 20:20:40 +00:00
2018-10-08 17:34:43 +00:00
```
</div>
</section>
## Solution
<section id='solution'>
```js
// solution required
```
</section>