Copying an array of objects into another array in javascript (Deep Copy) Copying an array of objects into another array in javascript (Deep Copy) google-chrome google-chrome

Copying an array of objects into another array in javascript (Deep Copy)


Try the following

// Deep copyvar newArray = jQuery.extend(true, [], oldArray);

For more details check this question out What is the most efficient way to deep clone an object in JavaScript?


As mentioned Here .slice(0) will be effective in cloning the array with primitive type elements. However in your example tags array contains anonymous objects. Hence any changes to these objects in cloned array are reflected in tags array.

@dangh's reply above derefences these element objects and create new ones.

Here is another thread addressing similar situation