19

Here is a sample of what I would like to do

function test(r){
  var arr = ['d','e','f'];
  r.push(arr);
  /*
  More Code
  */
  return r;
}
var result = test(['a','b','c']);
alert(result.length);//I want this to alert 6

What I need to do is pass in an array and attach other arrays to the end of it and then return the array. Because of passing by reference I cannot use array.concat(array2);. Is there a way to do this without using something like a for loop to add the elements one by one. I tried something like r.push(arr.join()); but that did not work either. Also, I would like the option of having objects in the arrays so really the r.push(arr.join()); doesn't work very well.

1
  • These days, you can use an iterable, and join arrays logically, without any data copy. Check out this code. Commented Oct 22, 2024 at 10:33

3 Answers 3

62
>>> var x = [1, 2, 3], y = [4, 5, 6];
>>> x.push.apply(x, y) // or Array.prototype.push.apply(x, y)
>>> x
[1, 2, 3, 4, 5, 6]

Alternatively using destructuring you can now do this

//generate a new array
a=[...x,...y];
//or modify one of the original arrays
x.push(...y);
Sign up to request clarification or add additional context in comments.

4 Comments

Interesting use of apply to push all members of an array onto another array +1
I had seen the apply method before, but never really understood it (note to self need to study up on apply), but it certainly makes a concise way of doing what I wanted. How does the second method differ from the first one though? Using the Array.prototype instead of x.push.apply.
Both are equivalent. Calling x.push(y) means "call the function x.push with scope x and the arguments [y]. In this case x.push resolves to Array.prototype.push, thus: Array.prototype.push.apply(x, [y]). I only mentioned the second form as it's sometimes preferable to only reference x once.
x.push.apply(x, y) solved it for me, especially the second x even though I don't understand why it's like that
2
function test(r){
  var _r = r.slice(0), // copy to new array reference
      arr = ['d','e','f'];

  _r = _r.concat(arr); // can use concat now

  return _r;
}
var result = test(['a','b','c']);
alert(result.length); // 6

1 Comment

Wrong, the array you return is not the modified array that was passed to the function, it is a copy
1

This is emulbreh's answer, I'm just posting the test I did to verify it. All credit should go to emulbreh

// original array
var r = ['a','b','c'];

function test(r){
  var arr = ['d','e','f'];
  r.push.apply(r, arr);

  /*
  More Code
  */
  return r;
}
var result = test( r );
console.log( r ); // ["a", "b", "c", "d", "e", "f"]
console.log( result === r ); // the returned array IS the original array but modified

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.