简体   繁体   English

使用splice从数组中删除对象

[英]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 我想在用户中删除拥有属性videoGameisplayer属性设置为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 这里是一个关于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. 也许Array.filter()方法正是您所寻找的。

$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. 在功能上等同于在Mike Brant的建议中调用Array.filter(),而不创建数组的额外副本。

If you want to follow the "style" of your current plunker here's a snippet that works. 如果你想跟随你当前的plu​​nker的“风格”这里有一个有效的片段。

  $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. 为了记录,我认为Mike Brant的解决方案更清晰。 Also, your data seems unnecessarily complicated. 此外,您的数据似乎不必要地复杂化。 For instance, your $scope.data is an array with only one object in it. 例如,$ scope.data是一个只包含一个对象的数组。 Also the videoGame object is wrapped in an array as well. 此外,videoGame对象也包含在一个数组中。 This adds the [0] requirement and makes your code brittle. 这会增加[0]要求并使您的代码变得脆弱。

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. 从长远来看,最有可能让你的生活更轻松。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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