简体   繁体   中英

Remove object from array with splice

i knows that there's tons of examples answering to my question but i'm still having problem to do it:

Here's my data:

$scope.data = [
    {
      users:[
        {name: 'Stephen', age: 50, dev: 'js', car: 'red', shoes:'green', happy:true, videoGame:[{isPlayer:true, console:'PS3'}]},
        {name: 'Stephen', age: 28, dev: 'angular', car: 'gold', shoes:'silver', happy:true, videoGame:[{isPlayer:false, console:'none'}]},
        {name: 'Adam', age: 43, dev: 'php', car: 'blue', shoes:'yellow', happy:true, videoGame:[{isPlayer:true, console:'XBOX'}]},
        {name: 'John', age: 27, dev: 'java', car: 'green', shoes:'black', happy:true, videoGame:[{isPlayer:true, console:'PC'}]},
        {name: 'Steve', age: 29, dev: 'ruby', car: 'white', shoes:'blue', happy:true, videoGame:[{isPlayer:false, console:'none'}]},
        {name: 'Pablo', age: 34, dev: 'java', car: 'pink', shoes:'red', happy:false, videoGame:[{isPlayer:true, console:'GAMEBOY'}]}
      ],
      futureUsers:[
        {name: 'Walter', age: 56, dev: 'js', car: 'red', shoes:'green', happy:true},
        {name: 'Jessi', age: 27, dev: 'angular', car: 'gold', shoes:'silver', happy:true},
        {name: 'Arnold', age: 34, dev: 'php', car: 'blue', shoes:'yellow', happy:true},
        {name: 'Bill', age: 67, dev: 'java', car: 'green', shoes:'black', happy:true},
        {name: 'Josh', age: 21, dev: 'ruby', car: 'white', shoes:'blue', happy:true},
        {name: 'Sam', age: 31, dev: 'java', car: 'pink', shoes:'red', happy:false}
      ]
    }
      ];

I want to remove in users the user who have the property videoGame with isplayer property set to false

Here's what i'm trying:

  $scope.removeNotPlayer = function(){
      for(var i=0; i<$scope.data.users.length; i++){
        if($scope.data[i].users.videoGame === false){
            $scope.data.splice(i, 1);
        }
      }
      return $scope.data;
  };

here's a link to a plunker: http://plnkr.co/edit/u4f8Vnds91zu8MNttHr0?p=preview

Any help would be kind, i'm a beginner forgive my question please.

Perhaps Array.filter() method is what you are looking for.

$scope.data[0].users = $scope.data[0].users.filter(function(val) {
    return (val.videoGame[0].isPlayer === true);
});
function filterInPlace(x, fun) {
    var j=0;

    for (var i=0; i<x.length; i++)
        if (fun(x[i])) x[j++] = x[i];

    while (x.length > j)
        x.pop();
}

filterInPlace($scope.data[0].users, function(v) {    
    return v.videoGame[0].isPlayer
});

Functionally equivalent to calling Array.filter() as in Mike Brant's suggestion, without creating an extra copy of the array.

If you want to follow the "style" of your current plunker here's a snippet that works.

  $scope.removeNotPlayer = function(){
      for(var i=0; i<$scope.data[0].users.length; i++){
        if($scope.data[0].users[i].videoGame[0].isPlayer === false){
            $scope.data[0].users.splice(i, 1);
        }
      }
      return $scope.data;
  };

For the record, I think Mike Brant's solution is cleaner. Also, your data seems unnecessarily complicated. For instance, your $scope.data is an array with only one object in it. Also the videoGame object is wrapped in an array as well. This adds the [0] requirement and makes your code brittle.

If you have the flexibility, changing your data to:

$scope.data = {
      users:[
        {name: 'Stephen', age: 50, dev: 'js', car: 'red', shoes:'green', happy:true, videoGame:{isPlayer:true, console:'PS3'}},
        {name: 'Stephen', age: 28, dev: 'angular', car: 'gold', shoes:'silver', happy:true, videoGame:{isPlayer:false, console:'none'}},
        {name: 'Adam', age: 43, dev: 'php', car: 'blue', shoes:'yellow', happy:true, videoGame:{isPlayer:true, console:'XBOX'}},
        {name: 'John', age: 27, dev: 'java', car: 'green', shoes:'black', happy:true, videoGame:{isPlayer:true, console:'PC'}},
        {name: 'Steve', age: 29, dev: 'ruby', car: 'white', shoes:'blue', happy:true, videoGame:{isPlayer:false, console:'none'}},
        {name: 'Pablo', age: 34, dev: 'java', car: 'pink', shoes:'red', happy:false, videoGame:{isPlayer:true, console:'GAMEBOY'}}
      ],
      futureUsers:[
        {name: 'Walter', age: 56, dev: 'js', car: 'red', shoes:'green', happy:true},
        {name: 'Jessi', age: 27, dev: 'angular', car: 'gold', shoes:'silver', happy:true},
        {name: 'Arnold', age: 34, dev: 'php', car: 'blue', shoes:'yellow', happy:true},
        {name: 'Bill', age: 67, dev: 'java', car: 'green', shoes:'black', happy:true},
        {name: 'Josh', age: 21, dev: 'ruby', car: 'white', shoes:'blue', happy:true},
        {name: 'Sam', age: 31, dev: 'java', car: 'pink', shoes:'red', happy:false}
      ]
    };

Would most likely make your life easier in the long run.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM