我知道有很多例子可以回答我的问题,但我仍有问题要做:
这是我的数据:
$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}
]
}
];
我想在用户中删除拥有 videoGame 属性且 isplayer 属性设置为 false 的用户
以下是我正在尝试的内容:
$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;
};
这是一个关于plunker的链接:http://plnkr.co/edit/u4f8Vnds91zu8MNttHr0?p=preview
任何帮助都会很友好,我是初学者请原谅我的问题。
答案 0 :(得分:6)
或许Array.filter()
方法正是您所寻求的。
$scope.data[0].users = $scope.data[0].users.filter(function(val) {
return (val.videoGame[0].isPlayer === true);
});
答案 1 :(得分:1)
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
});
在功能上等同于调用Array.filter(),如Mike Brant的建议,而不创建数组的额外副本。
答案 2 :(得分:1)
如果你想跟随你当前的plunker的“风格”这里有一个有效的片段。
$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;
};
为了记录,我认为Mike Brant的解决方案更清晰。 此外,您的数据似乎不必要地复杂化。例如,$ scope.data是一个只包含一个对象的数组。此外,videoGame对象也包含在一个数组中。这会增加[0]要求并使您的代码变得脆弱。
如果您具有灵活性,请将数据更改为:
$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}
]
};
从长远来看,最有可能让你的生活变得更轻松。