2.7 KiB
2.7 KiB
id | title | challengeType | videoUrl | localeTitle |
---|---|---|---|---|
587d7b89367417b2b2512b49 | Use Destructuring Assignment to Assign Variables from Objects | 1 | Usar asignación de destrucción para asignar variables de objetos |
Description
var voxel = {x: 3.6, y: 7.4, z: 6.54};Aquí está la misma declaración de asignación con la sintaxis de desestructuración ES6:
var x = voxel.x; // x = 3.6
var y = voxel.y; // y = 7.4
var z = voxel.z; // z = 6.54
const {x, y, z} = voxel; // x = 3.6, y = 7.4, z = 6.54Si, por el contrario, desea almacenar los valores de
voxel.x
en a
, voxel.y
en b
, y voxel.z
en c
, también tiene esa libertad. const {x: a, y: b, z: c} = vóxel // a = 3.6, b = 7.4, c = 6.54Puede leerlo como "obtener el campo
x
y copiar el valor en a
", y así sucesivamente. Instructions
AVG_TEMPERATURES
, y asigne un valor con la clave tomorrow
a tempOfTomorrow
en línea. Tests
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");'
Challenge Seed
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
Solution
// solution required