I am splicing a list of phone types. I have an original list defined by $scope.phoneTypeList. In my actual app I'm obtaining the list through AJAX but I've defined it as an object for this example.
I then have an empty list called $scope.addPhoneType. I assign the data from phoneTypelist into $scope.addPhoneType then run a for loop. The for loop takes an array of a users phone types. If it is in the list it will splice the addPhoneType. Eg if the user has DEFAULT it will remove it from addPhoneType.
The issue I'm having is when I splice the addPhoneType it also splices phoneTypeList. That is for the example phoneTypeList becomes =["DDI", "MOBILE","FAX"] where it shouldn't be touched at all.
It's like its binded to addPhoneType. I've never come across this before? Is this normal behaviour? Does anyone have a work around? Thanks in advance, any advice appreciated.
My Code:
$scope.editCompany = function(){
$scope.phone = [{phone_type_id:"DEFAULT",number:"1123"}]
$scope.phoneTypeList = ["DDI", "DEFAULT", "MOBILE","FAX"];
$scope.addPhoneType =[];
$scope.addPhoneType = $scope.phoneTypeList;
for (x=0;x<$scope.phone.length;x++){
var phoneType = $scope.phone[x].phone_type_id;
var index = $scope.addPhoneType.indexOf(phoneType);
if (index >= 0){
$scope.addPhoneType.splice(index,1);
}
}
console.log($scope.phoneTypeList);
}