Join the Stack Overflow Community
Stack Overflow is a community of 6.6 million programmers, just like you, helping each other.
Join them; it only takes a minute:
Sign up

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
}]
share|improve this question
    
you're modifying an array as you iterate over its index. Not sure if that is the cause but you should never do that. at least break the loop if you do. But you should prob use Array.filter – lonewarrior556 May 18 '16 at 20:08
    
This really has nothing to do with angular – George Jempty May 18 '16 at 20:23
up vote 1 down vote accepted
$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;

share|improve this answer
    
Thanks for the help it worked! – hussain May 19 '16 at 14:36

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.

share|improve this answer
    
Avoid using let if question is not tagged es6? – lonewarrior556 May 18 '16 at 20:13
    
You're right. Just a typescript habit of mine. TS transpiles let into var. – Frode May 18 '16 at 20:18
1  
and now stack overflow has 3 more questions... let? es6? typescript? :) – lonewarrior556 May 18 '16 at 20:22
1  
transpile? :-)) – Frode May 18 '16 at 20:24

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.