Files
freeCodeCamp/curriculum/challenges/german/02-javascript-algorithms-and-data-structures/es6/use-destructuring-assignment-with-the-rest-parameter-to-reassign-array-elements.md
2022-12-20 09:00:39 -06:00

92 lines
2.5 KiB
Markdown

---
id: 587d7b8a367417b2b2512b4c
title: >-
Destructuring via rest elements
challengeType: 1
forumTopicId: 301218
dashedName: >-
use-destructuring-assignment-with-the-rest-parameter-to-reassign-array-elements
---
# --description--
In manchen Situationen, in denen es um die Destrukturierung von Arrays geht, möchten wir vielleicht die restlichen Elemente in einem separaten Array sammeln.
Das Ergebnis ist ähnlich wie bei `Array.prototype.slice()`, wie unten gezeigt:
```js
const [a, b, ...arr] = [1, 2, 3, 4, 5, 7];
console.log(a, b);
console.log(arr);
```
Die Konsole würde die Werte `1, 2` und `[3, 4, 5, 7]` anzeigen.
Die Variablen `a` und `b` entnehmen den ersten und zweiten Wert aus dem Array. After that, because of the rest syntax presence, `arr` gets the rest of the values in the form of an array. Das Rest-Element funktioniert nur als letzte Variable in der Liste richtig. As in, you cannot use the rest syntax to catch a subarray that leaves out the last element of the original array.
# --instructions--
Use a destructuring assignment with the rest syntax to emulate the behavior of `Array.prototype.slice()`. `removeFirstTwo()` should return a sub-array of the original array `list` with the first two elements omitted.
# --hints--
`removeFirstTwo([1, 2, 3, 4, 5])` should be `[3, 4, 5]`
```js
const testArr_ = [1, 2, 3, 4, 5];
const testArrWORemoved_ = removeFirstTwo(testArr_);
assert(testArrWORemoved_.every((e, i) => e === i + 3) && testArrWORemoved_.length === 3);
```
`removeFirstTwo()` should not modify `list`
```js
const testArr_ = [1, 2, 3, 4, 5];
const testArrWORemoved_ = removeFirstTwo(testArr_);
assert(testArr_.every((e, i) => e === i + 1) && testArr_.length === 5);
```
`Array.slice()` sollte nicht verwendet werden.
```js
(getUserInput) => assert(!getUserInput('index').match(/slice/g));
```
Die Destrukturierung auf `list` sollte verwendet werden.
```js
assert(
__helpers
.removeWhiteSpace(code)
.match(/\[(([_$a-z]\w*)?,){1,}\.\.\.shorterList\]=list/i)
);
```
# --seed--
## --seed-contents--
```js
function removeFirstTwo(list) {
// Only change code below this line
const shorterList = list; // Change this line
// Only change code above this line
return shorterList;
}
const source = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10];
const sourceWithoutFirstTwo = removeFirstTwo(source);
```
# --solutions--
```js
function removeFirstTwo(list) {
const [, , ...shorterList] = list;
return shorterList;
}
const source = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10];
const sourceWithoutFirstTwo = removeFirstTwo(source);
```