Home Copying array by value in JavaScript
Reply: 0

Copying array by value in JavaScript

user372
1#
user372 Published in May 21, 2018, 11:18 am

When copying an array in JavaScript to another array:

var arr1 = ['a','b','c'];
var arr2 = arr1;
arr2.push('d');  //Now, arr1 = ['a','b','c','d']

I realized that arr2 refers to the same array as arr1, rather than a new, independent array. How can I copy the array to get two independent arrays?

You need to login account before you can post.

About| Privacy statement| Terms of Service| Advertising| Contact us| Help| Sitemap|
Processed in 0.442983 second(s) , Gzip On .

© 2016 Powered by mzan.com design MATCHINFO