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

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

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. La asignación de destrucción es una sintaxis especial para asignar cuidadosamente valores tomados directamente de un objeto a variables. Considere el siguiente código ES5:
var voxel = {x: 3.6, y: 7.4, z: 6.54};
var x = voxel.x; // x = 3.6
var y = voxel.y; // y = 7.4
var z = voxel.z; // z = 6.54
Aquí está la misma declaración de asignación con la sintaxis de desestructuración ES6:
const {x, y, z} = voxel; // x = 3.6, y = 7.4, z = 6.54
Si, 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.54
Puede leerlo como "obtener el campo x y copiar el valor en a ", y así sucesivamente.

Instructions

Utilice la desestructuración para obtener la temperatura promedio para mañana desde el objeto de entrada 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