mardi 21 avril 2015

Javascript remove object from array with splice

Vote count: 0

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.length; i++){
        if($scope.data[i].users.videoGame === false){
            $scope.data.splice(i, 1);
            break;
        }
      }
      return $scope.data;
  };

here's a link to a plunker:http://ift.tt/1yMUbuh

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

asked 40 secs ago



Javascript remove object from array with splice

Aucun commentaire:

Enregistrer un commentaire