Intermediate Algorithm Scripting - Sorted Union

Please what is the difference between
[…arrays]; and
.concat(…arrays);

Thanks

Depending on the items of arrays the results of both might be the same: a new array with the same items as arrays.
If you want to make a new array consisting of the elements of two arrays, you could use the same methods:

[...arr1, ...arr2]
arr1.concat(arr2)
[].concat(arr1, arr2)

I don’t know if you realized what you do with [].concat(...arrays)
Be sure you really want to unpack the array, an example where it’s different:

const arrays = [[1,2,3], ["a","b"])
[...arrays]          // [[1,2,3], ["a","b"])
[].concat(arrays)    // [[1,2,3], ["a","b"])
[].concat(...arrays) // [1, 2, 3, "a", "b"]

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.