Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
学ぶ ES6 Spread Operator | Getting Started: ES6
/
Introduction to React (Stage)

bookES6 Spread Operator

メニューを表示するにはスワイプしてください

The spread operator () can be easily used to assign an array's values to another array.

123
let arr = [5, 6, 7, 8, 9]; let numbers = [1, 2, 3, 4, ...arr]; console.log (numbers);
copy

Changing the position of the...arr term will change the position of data in the output:

123
let arr = [5, 6, 7, 8, 9]; let numbers = [1, 2, ...arr, 3, 4]; console.log (numbers);
copy

The spread operator unpacks the array arr elements and places them at the location. This can be used in many flexible ways, like passing arguments to a function:

12345
let values = [1, 7, 9]; function sum(a, b, c) { return a + b + c; } console.log (sum(...values));
copy

Task

Complete the following code by creating a new array called numbers and append the elements of both even and odd arrays (in this order) to the numbers array. Do it in a single line of code.

question-icon

Fill in the gaps.

const even = [2, 4, 6, 8];
const odd = [1, 3, 5, 9];
// Write code below this line
= [,]
// White code above this line
console.log (numbers.sort ());
[1, 2, 3, 4, 5, 6, 8, 9]

クリックまたはドラッグ`n`ドロップして空欄を埋めてください

すべて明確でしたか?

どのように改善できますか?

フィードバックありがとうございます!

セクション 1.  7

AIに質問する

expand

AIに質問する

ChatGPT

何でも質問するか、提案された質問の1つを試してチャットを始めてください

セクション 1.  7
some-alt