8

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?

1
  • Have a look at the link. It doesn't matter what I do, it's exactly the same as shown on the other page. Commented May 21, 2013 at 21:17

3 Answers 3

10

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.

Sign up to request clarification or add additional context in comments.

2 Comments

It also states that "Object references (and not the actual object): concat copies object references into the new array. Both the original and new array refer to the same object. That is, if a referenced object is modified, the changes are visible to both the new and original arrays."
@Tamas That is talking about the objects in the arrays, not the arrays themselves - but, yes.
10

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.

2 Comments

I won't be able to accept 2 answers but this was helpful as well Frits, thank you!
Does this require Array.prototype or can it be done with just an array?
1

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] 

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.