Reputation: 7093
i wanted to delete index from array if rest service response return true , so in below case lets say we have true from rest service i wanted to delete object where id is 'RA_ATTST_LANGUAGE` from array. I tried below code but its not deleting what is missing ?
main.js
MessageAdminNotificationFactory.getAttestationLanValidation().then(function(response){
var data = response.data;
console.log('attestation',data);
if(data){
angular.forEach($scope.adminDataSource,function(value,$index){
if(value.id === 'RA_ATTST_LANGUAGE'){
$scope.adminDataSource.splice($index, 1);
}
console.log('dropdown',value.id);
});
}
});
$scope.adminDataSource = [{
"uid": null,
"index": 0,
"selected": null,
"expanded": null,
"id": "RA_PLTFRM_NOTIF",
"text": "Platform Maintenance Notification",
"parentId": null,
"items": null
}, {
"uid": null,
"index": 0,
"selected": null,
"expanded": null,
"id": "RA_ATTST_LANGUAGE",
"text": "Attestation Language",
"parentId": null,
"items": null
}]
Upvotes: 0
Views: 1351
Reputation: 4479
$scope.adminDataSource = $scope.adminDataSource.filter(
function(value){
return value.id !== 'RA_ATTST_LANGUAGE';
})
Array.filter is the way to go. filters out anything that evaluates to false;
Upvotes: 1
Reputation: 3455
Iterate the array from end to start
for(var i=$scope.adminDataSource.length-1;i>=0;i--){
var value = $scope.adminDataSource[i];
if(value.id === 'RA_ATTST_LANGUAGE') {
$scope.adminDataSource.splice(i,1);
}
}
This way it doesn't matter if the array shrinks while you iterate.
Upvotes: 0