-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
  • 21,535
  • 7
  • 47
  • 56
Jay
  • 9,189
  • 12
  • 56
  • 96

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
  • 133,658
  • 13
  • 134
  • 193