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

时间:2015-11-26 14:56:12

标签: javascript jquery arrays angularjs

我有一个我用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/

3 个答案:

答案 0 :(得分:2)

你的情景对我来说不太清楚。

如果您希望全部选择按钮的行为类似于所有链接,那么这是您的解决方案:

$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);
        }
    }

};

答案 1 :(得分:0)

您的代码有效,但您需要创建其他

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);
                }
            }
        };
}

答案 2 :(得分:0)

Netzach对这个错误是正确的。

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

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

这是一个工作示例的jsfiddle:

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