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);
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.
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 .
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.
Array1.length = 0; // Clear contents Array1.push.apply(Array1, Array2); // Append new contents
Array1.splice(0, Array1.length, ...Array2);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With