I have two arrays, one comes as a reference (parameter) from a function, and the other is created as part of the function - exactly the same scenario as described here:
Add two arrays without using the concat method
I was using the push.apply() method as per the suggestion above, but can someone please explain to me, why I can't use concat() to merge two arrays if the array is sent into the function as a reference?
Example 1: In this example, the method concat () concatenates all the three arrays into one array which it return as the answer. Example 2: In this example, the method concat () concatenates all the arguments passed to the method with the given array into one array which it return as the answer.
The concat () method takes in an arbitrary number of arrays and/or values as arguments. Returns a newly created array after merging all arrays/values passed in the argument. The concat () method first creates a new array with the elements of the object on which the method is called.
The concat () method returns a new array, containing the joined arrays. The concat () method does not change the existing arrays. ... Required. The arrays to be joined. The joined arrays.
Below is the example of Array concat () method to join three arrays. The arr.concat () method is used to merge two or more arrays together. This method does not alter the original arrays passed as arguments. Parameters: The parameters to this method are the arrays or the values that need to be added to the given array.
Refer to Array.concat on MDN:
Any operation on the new array will have no effect on the original arrays, and vice versa.
This makes it behave differently from Array.push.apply
which will mutate the original Array object - the return value of Array.concat
must be used. Otherwise, it works as explained in the MDN link above.
If you use concat
the original array will be unmodified. If you have a reference to it you wont see the new elements.
var arr1 = [ "a", "b" ];
var arr2 = [ "c", "d" ];
arr1.push.apply(arr1, arr2);
Basically does this:
[ "a", "b" ].push("c", "d");
apply
turns an array into a list of arguments. The first argument to apply
is the context
by the way, arr1
in this case since you want the push to apply to arr1
.
You can use concat
:
var arr1 = [ "a", "b" ];
var arr2 = [ "c", "d" ];
var arr3 = arr1.concat(arr2);
This leaves the original arr1
as it was. You've created a new array that has both arr1
and arr2
elements in it. If you have a reference to the original arr1
it will be unmodified. That might be a reason to not want to use concat
.
Let's say we have 2 arrays "a" and "b". Array.concat
method will return new instance of Array
"c" which represents concatenation between a and b without any mutation of a or b. Array.push
return last index of pushed element and mutate this
instance.
Since ES6 (or 15, not sure) it's possible to unpack parameters and you can use push to concatenate (without harmful code) as well
a = [1,2,3,4]; // a=[1,2,3,4];
b = [5,6,7,8]; // b=[5,6,7,8];
a.push(...b) // a=[1,2,3,4,5,6,7,8]; b=[5,6,7,8]
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With