1

Possible Duplicate:
Copying array by value in javascript

How to copy an array of objects to another array in Javascript?

var qwerty1 = arr;
var qwerty2 = arr;

Both qwerty1 and qwerty2 may look different but point to the same reference. I read somewhere that "assigning a boolean or string to a variable makes a copy of that value, while assigning an array or an object to a variable makes a reference to the value." So my two arrays post different operations return the same objects.

Any light in this regard?

Community
  • 1
  • 1
Premanshu
  • 616
  • 3
  • 14
  • 24

1 Answers1

2

The idiomatic way to copy an array in Javascript is to use concat:

var qwerty1 = arr.concat();
var qwerty2 = arr.concat();
rob mayoff
  • 375,296
  • 67
  • 796
  • 848