freeCodeCamp/guide/chinese/certifications/javascript-algorithms-and-d.../es6/use-destructuring-assignmen.../index.md

1.5 KiB
Raw Blame History

title localeTitle
Use Destructuring Assignment with the Rest Operator to Reassign Array Elements 使用与Rest运算符的Destructuring Assignment重新分配数组元素

使用与Rest运算符的Destructuring Assignment重新分配数组元素

请记住rest运算符允许可变数量的参数。在这个挑战中你必须摆脱数组的前两个元素。

提示1

将前两个元素分配给两个随机变量。

提示2

将数组的剩余部分设置为...arr

=======

提示1

使用destructuring来创建arr变量。

function removeFirstTwo(list) { 
  "use strict"; 
  // change code below this line 
  const [arr] = list; // change this 
  // change code above this line 
  return arr; 
 } 

提示2

list参数传播到arr

function removeFirstTwo(list) { 
  "use strict"; 
  // change code below this line 
  const [...arr] = list; // change this 
  // change code above this line 
  return arr; 
 } 

提示3

使用,,排除arr数组的前两个元素。

function removeFirstTwo(list) { 
  "use strict"; 
  // change code below this line 
  const [,,...arr] = list; // change this 
  // change code above this line 
  return arr; 
 } 

剧透警报 - 提前解决!

function removeFirstTwo(list) { 
  "use strict"; 
  // change code below this line 
  const [a, b, ...arr] = list; 
  // change code above this line 
  return arr; 
 }