-1

I copied an array to a temporary variable tempPropertyValuesArray

And then when I cleared the original array $scope.advancedSearch.businessCard.propertyValues

It also cleared the tempPropertyValuesArray

I am surprised. Is this the expected behavior ?

Javascript:

var tempPropertyValuesArray = $scope.advancedSearch.businessCard.propertyValues;
$log.debug("tempPropertyValuesArray 1 : " +tempPropertyValuesArray);
$scope.advancedSearch.businessCard.propertyValues.length = 0;
$log.debug("tempPropertyValuesArray 2 : " +tempPropertyValuesArray);

Logs:

tempPropertyValuesArray 1 : [object Object],[object Object] 
tempPropertyValuesArray 2 : 
Andreas
  • 20,797
  • 7
  • 44
  • 55
Jay
  • 8,607
  • 10
  • 52
  • 90

1 Answers1

4

In a Javascript I copied an array to a temporary variable tempPropertyValuesArray

You didn't copy it, you made a reference to it. Changing the reference, changes the original object.

If you really want a copy, do this:

var tempPropertyValuesArray 
      = $scope.advancedSearch.businessCard.propertyValues.slice();
Jamiec
  • 128,537
  • 12
  • 134
  • 188