32

eg:

var Array1=array(1,2,3,4,5,6);
var Array2=array(7,8,9,10,11,12);

after replacing Array2 with Array1 values Resulting array should be

var Array1=array(7,8,9,10,11,12);
Hassan Imam
  • 21,956
  • 5
  • 41
  • 51
SUGU
  • 407
  • 1
  • 5
  • 8
  • ok so what is the problem.?Array1 = Array2; – PPB Jun 04 '15 at 10:00
  • 1
    I believe the problem with doing that is that now both arrays are referenced. Changing a value in Array1 will also change the same value in Array2. Even if OP doesn't mention it. – Jeremy Thille Jun 04 '15 at 10:05
  • did you even try anything ? In this case there's a big chance the first thing you would have tried was actually the answer... – Laurent S. Jun 04 '15 at 10:05

3 Answers3

59

Prior ES6 (using push):

Array1.length = 0;                  // Clear contents
Array1.push.apply(Array1, Array2);  // Append new contents


Post ES6 (using splice):

Array1.splice(0, Array1.length, ...Array2);
vsync
  • 118,978
  • 58
  • 307
  • 400
broofa
  • 37,461
  • 11
  • 73
  • 73
  • 4
    Huge difference between Andy and @broofa's answers. Both correct in difference contexts. Andy creates a new Array object with the same content as the second array, with a single reference to the new object, and discards the reference to the first array object. broofa's preserves the first array object, but changes the content to match the second array object. So any other references to the first array object elsewhere in the code see the new values. OP's need is unclear. – Terry Brown Jun 21 '19 at 20:08
  • 1
    This should be the preferred answer, because it actually answers the question of replacing the whole content of the array, which in turn is reflected in all references of the array elsewhere in the code. – omostan Sep 06 '20 at 09:25
  • TBF I did write that 7 years ago. Quite a lot has changed. :) @TerryBrown – Andy Mar 01 '22 at 00:54
29

Use slice:

Array1 = Array2.slice(0);

This will take a copy of Array2, not make a reference to it, so if you make changes to Array2 they won't be reflected in Array1.

DEMO

Andy
  • 61,948
  • 13
  • 68
  • 95
  • 5
    This does not replace the actual array, but rather replaces the pointer of the variable to a new Array, which might not be desirable, like in a case where you cannot point to another location in memory because other code parts already refer to the original pointer, thus the first answer here might be preferable – vsync Nov 12 '19 at 21:09
3

in this example, it is possible to exclude the value of an array of type CONST, and the value of an array is passed to or another. I had this problem.

fields = [1,2,3,4]

var t = fields.filter(n => {
  return n != 4;
});

// t => [1,2,3]

fields.splice(0, fields.length, ...t);

console.log(fields);//[1,2,3]
Bruno Araujo
  • 435
  • 4
  • 15
  • 3
    I would recommend that you add some explanatory text to your answer, or you risk having it deleted as 'low quality' (even though it may be a correct answer)! – Adrian Mole Mar 02 '20 at 18:45