Files
freeCodeCamp/curriculum/challenges/japanese/02-javascript-algorithms-and-data-structures/es6/use-destructuring-assignment-with-the-rest-parameter-to-reassign-array-elements.md
2022-01-20 20:30:18 +01:00

2.3 KiB

id, title, challengeType, forumTopicId, dashedName
id title challengeType forumTopicId dashedName
587d7b8a367417b2b2512b4c 分割代入で残余引数を使用して配列要素を割り当て直す 1 301218 use-destructuring-assignment-with-the-rest-parameter-to-reassign-array-elements

--description--

配列の分割において、残りの要素を別の配列にまとめたいことがあります。

次に示すように、Array.prototype.slice() に似た結果になります。

const [a, b, ...arr] = [1, 2, 3, 4, 5, 7];
console.log(a, b);
console.log(arr);

コンソールには、値 1, 2[3, 4, 5, 7] が表示されます。

変数 ab は配列の最初と 2 番目の値を受け取ります。 その後は残余引数になっているため、arr は残りの値を配列の形で受け取ります。 残余引数は、リスト内の最後の変数としてのみ正しく動作します。 したがって、残余引数を使用して元の配列の最後の要素を取り除いた部分配列を取得することはできません。

--instructions--

分割代入と残余変数を使用して、Array.prototype.slice() の効果を実現してください。arr が、元の配列 source から最初の 2 つの要素を省いた部分配列となるようにします。

--hints--

arr[3,4,5,6,7,8,9,10] である必要があります。

assert(arr.every((v, i) => v === i + 3) && arr.length === 8);

source[1,2,3,4,5,6,7,8,9,10] である必要があります。

assert(source.every((v, i) => v === i + 1) && source.length === 10);

Array.slice() は使用しないでください。

(getUserInput) => assert(!getUserInput('index').match(/slice/g));

list の分割を使用する必要があります。

assert(
  __helpers
    .removeWhiteSpace(code)
    .match(/\[(([_$a-z]\w*)?,){1,}\.\.\.arr\]=list/i)
);

--seed--

--seed-contents--

const source = [1,2,3,4,5,6,7,8,9,10];
function removeFirstTwo(list) {
  // Only change code below this line
  const arr = list; // Change this line
  // Only change code above this line
  return arr;
}
const arr = removeFirstTwo(source);

--solutions--

const source = [1,2,3,4,5,6,7,8,9,10];
function removeFirstTwo(list) {
  const [, , ...arr] = list;
  return arr;
}
const arr = removeFirstTwo(source);