如果它具有某个键/值,则跳过一个值

时间:2014-12-15 20:04:15

标签: javascript angularjs

这里有一个奇怪的问题 - 所以我会尽可能清楚地解释这个问题。

我有一个简单的ng-repeat,它将根据.active设置为true的键值来显示内容。我让用户滚动浏览内容,其中一些箭头按钮绑定到某些ng-clicks。这很好用,但是我想从数组中排除一个项目,如果它的关键值是side =' help'附在它上面。所以基本上我希望箭头点击在某种意义上跳过它。不幸的是,我无法控制数组中的帮助项目。所以这是点击功能

//flip right
$scope.flipRight = function(index, parent){
    var idx = index + 1;
    if (idx >= $scope.contentHere[parent].sides.length) {
       idx = 0;
    }
    $scope.contentHere[parent].sides[index].active = false;
    $scope.contentHere[parent].sides[idx].active = true;
};
//flip left
$scope.flipLeft = function(index, parent){
    var idx = index - 1;
    if (idx < 0) {
     idx = $scope.contentHere[parent].sides.length - 1;
    }
    $scope.contentHere[parent].sides[index].active = false;
    $scope.contentHere[parent].sides[idx].active = true;
};

所以基本上我想要说明的是,如果它有.side =&#39; help&#39;,如何让这个逻辑跳过该项目。我想过使用lodash来通过没有值的项来_filter数组,但是它将偏移索引,这样就无法工作了。我不知道如何处理这个问题(也许我正在考虑这个问题?),并且可以使用一些方向。

感谢您抽出宝贵时间阅读!

1 个答案:

答案 0 :(得分:5)

$scope.flipRight = function(index, parent){
var idx = index + 1;
if(idx >= $scope.contentHere[parent].sides.length){
   idx = 0;
}
if($scope.contentHere[parent].sides[idx] == 'help'){
     $scope.flipRight(idx, parent); //Added to skip over to next item
     $scope.contentHere[parent].sides[index].active = false; // Added for the first item does not turn .active to false Issue
     return; // Added to skip execution of following line of codes incase of recursion
}
$scope.contentHere[parent].sides[index].active = false;
$scope.contentHere[parent].sides[idx].active = true;
};

//flip left
$scope.flipLeft = function(index, parent){
var idx = index - 1;
if (idx < 0) {
 idx = $scope.contentHere[parent].sides.length - 1;
}
if($scope.contentHere[parent].sides[idx] == 'help'){
     $scope.flipLeft(idx, parent); //Added to skip over to next item
     $scope.contentHere[parent].sides[index].active = false; // Added for the first item does not turn .active to false Issue
     return; // Added to skip execution of following line of codes incase of recursion
}
$scope.contentHere[parent].sides[index].active = false;
$scope.contentHere[parent].sides[idx].active = true;
};