The following code works fine as arr2 is empty, and arr2 got the contents of arr1, but I’m really confused that if arr2 is not empty, like arr2 = [‘JUNE’, ‘JULY’], anybody can tell me how I can put arr1 and arr2 together? I hope I’ve made me clear, thanks.
Your code so far
const arr1 = ['JAN', 'FEB', 'MAR', 'APR', 'MAY'];
let arr2;
arr2 = [...arr1];
console.log(arr2);
Your browser information:
User Agent is: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/99.0.4844.84 Safari/537.36
Challenge: Use the Spread Operator to Evaluate Arrays In-Place
Link to the challenge:
RaymondGuo:
arr2 = [...arr1];
This line here says "ignore the contents of arr2
and replace it with a shallow copy of arr1
"
1 Like
As it stands, your code is replacing the value in arr2
. But if you modified the [...arr1]
to include arr2
somehow…
1 Like
There are a couple of ways to join arrays in JS. Here are 2 common ways:
let arr3 = arr2.concat(arr1)
// does not modify arr1
and arr2
arr2 = [...arr2, ...arr1]
// modifies arr2
arr3 = [...arr1, ...arr2]
// does not modify arr1
and arr2
2 Likes
technique1: arr1.concat(arr2)
technique2: const arr2 = [a, b, c, …arr1]
1 Like
system
Closed
October 14, 2022, 8:05pm
7
This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.