Adrian Skar 08bdc8640e fix(guide): ES6 destructuring assignment with the rest operator (#35681)
* fix(guide): ES6 destructuring assignment with the rest operator

Minor fixes/enhancements.

* Update guide/english/certifications/javascript-algorithms-and-data-structures/es6/use-destructuring-assignment-with-the-rest-operator-to-reassign-array-elements/index.md

Co-Authored-By: AdrianSkar <adrian@adrianskar.com>

* Update guide/english/certifications/javascript-algorithms-and-data-structures/es6/use-destructuring-assignment-with-the-rest-operator-to-reassign-array-elements/index.md

Co-Authored-By: AdrianSkar <adrian@adrianskar.com>

* Update guide/english/certifications/javascript-algorithms-and-data-structures/es6/use-destructuring-assignment-with-the-rest-operator-to-reassign-array-elements/index.md

Co-Authored-By: AdrianSkar <adrian@adrianskar.com>

* Fixes for solutions 

Copy/paste friendly code as discussed.

* Update guide/english/certifications/javascript-algorithms-and-data-structures/es6/use-destructuring-assignment-with-the-rest-operator-to-reassign-array-elements/index.md

Co-Authored-By: AdrianSkar <adrian@adrianskar.com>
2019-03-29 09:40:13 -07:00

2.0 KiB

title
title
Use Destructuring Assignment with the Rest Operator to Reassign Array Elements

Use Destructuring Assignment with the Rest Operator to Reassign Array Elements

Remember that the rest operator allows for variable numbers of arguments. In this challenge, you have to get rid of the first two elements of an array.

Hint 1:

Assign the first two elements to two random variables.

Hint 2:

Set the remaining part of the array to ...arr.

Hint 3:

Use destructuring to create the arr variable:

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

Hint 4:

Spread the list parameter values into arr.

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

Spoiler Alert - Solution Ahead!

You can use random variables to omit the first two values:

const source = [1,2,3,4,5,6,7,8,9,10];
function removeFirstTwo(list) {
"use strict";
  // change code below this line
  const [a, b, ...arr] = list; 
  // change code above this line
  return arr;
}
const arr = removeFirstTwo(source);
console.log(arr); // should be [3,4,5,6,7,8,9,10]
console.log(source); // should be [1,2,3,4,5,6,7,8,9,10];

Solution 2:

You can also exclude the first two elements of the arr array using ,,.

const source = [1,2,3,4,5,6,7,8,9,10];
function removeFirstTwo(list) {
  "use strict";
  // change code below this line
  const [,,...arr] = list; // change this
  // change code above this line
  return arr;
}
const arr = removeFirstTwo(source);
console.log(arr); // should be [3,4,5,6,7,8,9,10]
console.log(source); // should be [1,2,3,4,5,6,7,8,9,10];

Resources