I have two arrays
which contain certain values the first array
is predefined and the second array
equals the first array
. The second array
itself is a array
auxiliary which I use it to remove elements. The error I have is that when I delete elements of the second array
the elements of the first array
are also deleted, This is my code:
At the moment of verifying my array
is empty. How can I resolve this error? and here my jsfiddle
function Ctrl($scope) {
$scope.categorias = [{"id_categoria":2,"id_empresa":2},{"id_categoria":3,"id_empresa":2}];
$scope.categorias_Aux = $scope.categorias;
$scope.eliminar_categoria = function(id_categoria){
for (var i = 0; i < $scope.categorias_Aux.length; i++){
if ($scope.categorias_Aux[i].id_categoria == id_categoria) {
$scope.categorias_Aux.splice(i, 1);
break;
}
}
console.log(JSON.stringify($scope.categorias))
}
$scope.verificar = function(){
$scope.categorias_Aux = $scope.categorias;
console.log(JSON.stringify($scope.categorias_Aux))
}
}
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
<div ng-app>
<div ng-controller="Ctrl">
<table style="width:100%">
<tr>
<th>Nom</th>
<th>id</th>
<th>accion</th>
</tr>
<tr ng-repeat="i in categorias_Aux">
<td>nombre cat</td>
<td>{{i.id_categoria}}</td>
<td><input type="button" value="x" ng-click="eliminar_categoria(i.id_categoria)"></td>
</tr>
</table>
<input type="button" ng-click="verificar()">
</div>
</div>