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

ajm*_*jma 6 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;
};
Run Code Online (Sandbox Code Playgroud)

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

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

Mit*_*tra 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;
};
Run Code Online (Sandbox Code Playgroud)

  • 注意:您还需要考虑一个数组长度为1的情况和Zeroth索引本身,如果有一个值'help'即唯一值$ scope.contentHere [parent] .sides数组有'help' (3认同)
  • @ajmajmajma:尝试在return语句$ scope.contentHere [parent] .sides [index] .active = false之前添加以下行: (3认同)