ES6 Spread Operator
The spread operator (…
) can be easily used to assign an array's values to another array.
123let arr = [5, 6, 7, 8, 9]; let numbers = [1, 2, 3, 4, ...arr]; console.log (numbers);
Changing the position of the...arr
term will change the position of data in the output:
123let arr = [5, 6, 7, 8, 9]; let numbers = [1, 2, ...arr, 3, 4]; console.log (numbers);
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:
12345let values = [1, 7, 9]; function sum(a, b, c) { return a + b + c; } console.log (sum(...values));
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.
Tutto è chiaro?
Grazie per i tuoi commenti!
Sezione 1. Capitolo 7
Chieda ad AI
Chieda pure quello che desidera o provi una delle domande suggerite per iniziare la nostra conversazione