1

While using array.push() to add values at the end of an array in angularjs will it create a deep copy of the pushed value inside the array or not.

Felipe Sabino
  • 17,825
  • 6
  • 78
  • 112
kartik
  • 583
  • 8
  • 24

3 Answers3

2

No, you will be adding a reference to the original object to the array, not the value or a copy

var obj = { key: "value" }
var arr = [];
arr.push(obj);

obj === arr[0];
// true
Felipe Sabino
  • 17,825
  • 6
  • 78
  • 112
0

No, Deep copy can be done via angular.copy. For example.

var data = [{'name':'abc'}, {'name':'xyz'}];
var copiedOne = angular.copy(data);
Manish Singh
  • 518
  • 3
  • 13
-2

In Javascript objects are always passed by reference unless you make a copy of it yourself.

Dimitri L.
  • 4,499
  • 1
  • 15
  • 19