Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

I want to replace all values with another array values , both arrays are in same size [closed]

Tags:

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); 
like image 1000
SUGU Avatar asked Jun 04 '15 09:06

SUGU


People also ask

How do you replace an array element with another array element?

Replace an Element in an Array using Array.Use the indexOf() method to get the index of the element you want to replace. Call the Array. splice() method to replace the element at the specific index. The array element will get replaced in place.

How do I replace one array with another?

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 .

What method is used to create a new array using elements of another array?

The method arr. concat creates a new array that includes values from other arrays and additional items. It accepts any number of arguments – either arrays or values.


1 Answers

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); 
like image 102
broofa Avatar answered Sep 19 '22 15:09

broofa