繁体   English   中英

删除数组值(如果已存在)

[英]Remove array value if already exists javascript

我有一个用ng-repeat显示的值数组。 当我单击其中一个时,我将此值添加到另一个数组中。 如果已经存在,我将其删除。 在这里很好用。 但是我有一个按钮,可以在第二个按钮中按下所有数组。 它正在工作,但是即使值已经存在,我也可以无限次推送整个数组。 当然,如果我检查一个或两个值,然后按“全选”,则它必须同时选择所有值以及已经通过单选选择的值。 通过这种方式,这是带有jsfiddle的代码:

var myApp = angular.module('myApp',[]);

function MyCtrl($scope) {
    $scope.all_titles = [
        "Title 1",
        "Title 2",
        "Title 3",
        "Title 4"
        ];

    $scope.selection=[];

    $scope.getSelectedItem = function getSelectedItems(title) {
            var idx = $scope.selection.indexOf(title);
            // is currently selected
            if (idx > -1) {
                $scope.selection.splice(idx, 1);
            }
            // is newly selected
            else {
                if(Array.isArray(title)) {
                    for(var i=0;i<title.length;i++) {
                        $scope.selection.push(title[i]);
                    }
                } else {
                    $scope.selection.push(title);
                }
            }
        };
}

<div ng-controller="MyCtrl">
    <div>
        <button data-ng-click="getSelectedItem(all_titles)">
            Select all
        </button>
    </div>
    <div ng-repeat="title in all_titles">
        <a ng-click="getSelectedItem(title)">{{title}}</a>
    </div>

    <hr>
    <div>
        {{selection}}
    </div>
</div>

http://jsfiddle.net/HB7LU/20342/

您的情况对我不太清楚。

如果您希望全选按钮的行为就像单击所有链接一样 ,那么这是您的解决方案:

$scope.getSelectedItem = function getSelectedItems(title) {
            if(Array.isArray(title)) {
                for(var i=0;i<title.length;i++) {
                    $scope.pushIt(title[i]);
                }
            } else {
                $scope.pushIt(title);
            }

    };

$scope.pushIt = function pushIt(title) {
        var idx = $scope.selection.indexOf(title);
        // remove if already in array
        if (idx > -1) {
            $scope.selection.splice(idx, 1);
        } else {
            $scope.selection.push(title);
        }
    };

如果您希望全选按钮添加其余项目 ,那么这是您的解决方案:

$scope.getSelectedItem = function getSelectedItems(title) {

    if (Array.isArray(title)) {
        for (var i = 0; i < title.length; i++) {
            var idx = $scope.selection.indexOf(title[i]);
            // don't add if already in the array
            if (idx == -1) {
                $scope.selection.push(title[i]);
            }
        }
    } else {
        var idx = $scope.selection.indexOf(title);
        // is currently selected
        if (idx > -1) {
            $scope.selection.splice(idx, 1);
        } else {
            $scope.selection.push(title);
        }
    }

};

您的代码是可行的,但是您需要创建一个else

for(var i=0;i<title.length;i++) {
 var n_idx = $scope.selection.indexOf(title[i]);
 if( n_idx == -1){
   $scope.selection.push(title[i]);
 }else{
   $scope.selection.splice(n_idx, 1);
 }
}


var myApp = angular.module('myApp',[]);

//myApp.directive('myDirective', function() {});
//myApp.factory('myService', function() {});

function MyCtrl($scope) {
    $scope.all_titles = [
        "Title 1",
        "Title 2",
        "Title 3",
        "Title 4"
        ];

    $scope.selection=[];

    $scope.getSelectedItem = function getSelectedItems(title) {
            var idx = $scope.selection.indexOf(title);
            // is currently selected
            if (idx > -1) {
                $scope.selection.splice(idx, 1);
            }
            // is newly selected
            else {
                if(Array.isArray(title)) {
                    console.log("YES");
                    for(var i=0;i<title.length;i++) {
                        var n_idx = $scope.selection.indexOf(title[i]);
                        if( n_idx == -1){
                            $scope.selection.push(title[i]);
                        }else{
                            $scope.selection.splice(n_idx, 1);
                        }
                    }
                } else {
                    $scope.selection.push(title);
                }
            }
        };
}

Netzach关于该错误是正确的。

另外,我将通过使用哈希而不是遍历数组来优化代码。 然后,排除/包含该值的代码将如下所示:

    if($scope.selection[title]){
        delete $scope.selection[title];
    } else {
        $scope.selection[title] = true;
    }

这是工作示例的jsfiddle:

http://jsfiddle.net/HB7LU/20348/

暂无
暂无

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

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