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

2.4 KiB
Raw Blame History

id, title, challengeType, videoUrl, localeTitle
id title challengeType videoUrl localeTitle
587d7b89367417b2b2512b49 Use Destructuring Assignment to Assign Variables from Objects 1 使用解构分配从对象分配变量

Description

我们之前看到扩展运算符如何有效地扩展或解包数组的内容。我们也可以用对象做类似的事情。 解构赋值是一种特殊的语法用于将直接从对象获取的值整齐地分配给变量。请考虑以下ES5代码
var voxel = {x3.6y7.4z6.54};
var x = voxel.x; // x = 3.6
var y = voxel.y; // y = 7.4
var z = voxel.z; // z = 6.54
这是与ES6解构语法相同的赋值语句
const {xyz} =体素; // x = 3.6y = 7.4z = 6.54
相反,如果你想将voxel.x的值存储到a ,将voxel.yb ,将voxel.zc ,那么你也有这种自由。
const {xaybzc} =体素// a = 3.6b = 7.4c = 6.54
您可以将其读作“获取字段x并将值复制到a中”,依此类推。

Instructions

使用解构从输入对象AVG_TEMPERATURES获得明天的平均温度,并在tomorrow将关键值赋值给tempOfTomorrow

Tests

tests:
  - text: <code>getTempOfTmrw(AVG_TEMPERATURES)</code>应为<code>79</code>
    testString: 'assert(getTempOfTmrw(AVG_TEMPERATURES) === 79, "<code>getTempOfTmrw(AVG_TEMPERATURES)</code> should be <code>79</code>");'
  - text: 使用了重新分配的解构
    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