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

时间:2016-07-28 10:21:28

标签: javascript angularjs

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

我有另一个名为$scope.clickedBoxes的数组,我在其中输入了几个值。

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

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

2 个答案:

答案 0 :(得分:1)

没有这样的内置功能

你可以用这个

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

});

答案 1 :(得分:0)

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
}