繁体   English   中英

AngularJS检查两个数组是否具有不同的元素

[英]Angularjs check if two arrays have different elements

我有一个数组$scope.blinkingBoxes=[1,3,2]

我还有一个名为$scope.clickedBoxes数组,并且在其中推送了几个值。

现在if(angular.equals($scope.blinkingBoxes, $scope.clickedBoxes)){doSomething()}检查两个数组是否相同(即相同元素的顺序相同)

但是我想检查第二个数组是否不包含第一个数组中的任何元素并执行一些操作。 我该如何实现?

没有这样的内置功能

你可以用这个

angular.forEach(array1, function(value, key) {
angular.forEach(array2, function(value_1, key_1) {
    if (value === value_1) {
        // condition or action
    }
});

});

count = 0;
angular.forEach($scope.blinkingBoxes, function(value, key) { 
    if(value.indexOf($scope.clickedBoxes) == -1) {
        //not in same order or not same elements action goes here
        count++;
    }
});

if(count == $scope.blinkingBoxes.length) {
    //second array do not contain any element from first array, action goes here
}

暂无
暂无

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

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