Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why can't I concat an array reference in JavaScript?

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?

like image 808
Tamas Avatar asked May 21 '13 21:05

Tamas


People also ask

What is concat () in JavaScript with example?

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.

How do you concat an array in Java?

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.

What is the use of concat () method?

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.

How to join three arrays together in JavaScript?

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.


3 Answers

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.

like image 111
user2246674 Avatar answered Sep 18 '22 01:09

user2246674


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.

like image 40
Halcyon Avatar answered Sep 20 '22 01:09

Halcyon


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] 
like image 29
dorintufar Avatar answered Sep 20 '22 01:09

dorintufar