Files
freeCodeCamp/curriculum/challenges/chinese-traditional/02-javascript-algorithms-and-data-structures/es6/use-destructuring-assignment-to-pass-an-object-as-a-functions-parameters.md
Nicholas Carrigan (he/him) 3da4be21bb chore: seed chinese traditional (#42005)
Seeds the chinese traditional files manually so we can deploy to
staging.
2021-05-05 22:43:49 +05:30

1.7 KiB

id, title, challengeType, forumTopicId, dashedName
id title challengeType forumTopicId dashedName
587d7b8a367417b2b2512b4d 使用解構賦值將對象作爲函數的參數傳遞 1 301217 use-destructuring-assignment-to-pass-an-object-as-a-functions-parameters

--description--

在某些情況下,你可以在函數的參數裏直接解構對象。

請看以下代碼:

const profileUpdate = (profileData) => {
  const { name, age, nationality, location } = profileData;

}

上面的操作解構了傳給函數的對象。 這樣的操作也可以直接在參數裏完成:

const profileUpdate = ({ name, age, nationality, location }) => {

}

profileData 被傳遞到上面的函數時,從函數參數中解構出值以在函數內使用。

--instructions--

half 的參數進行解構賦值,僅將 maxmin 的值傳進函數。

--hints--

stats 的類型應該是一個 object

assert(typeof stats === 'object');

half(stats) 應該等於 28.015

assert(half(stats) === 28.015);

應該使用解構賦值。

assert(__helpers.removeWhiteSpace(code).match(/half=\({\w+,\w+}\)/));

應該使用解構參數。

assert(!code.match(/stats\.max|stats\.min/));

--seed--

--seed-contents--

const stats = {
  max: 56.78,
  standard_deviation: 4.34,
  median: 34.54,
  mode: 23.87,
  min: -0.75,
  average: 35.85
};

// Only change code below this line
const half = (stats) => (stats.max + stats.min) / 2.0; 
// Only change code above this line

--solutions--

const stats = {
  max: 56.78,
  standard_deviation: 4.34,
  median: 34.54,
  mode: 23.87,
  min: -0.75,
  average: 35.85
};

const half = ( {max, min} ) => (max + min) / 2.0;