简体   繁体   English

Angular.js比较两个数组中的元素

[英]Angularjs comparing elements from two arrays

I have one array something like following: 我有一个数组如下所示:

$scope.blinkedBoxes=[3,4,1,2,..]

It will have upto 8 elements in total (elements will be one of the numbers from 1,2,3,4). 它将总共有8个元素 (元素将是1,2,3,4中的数字之一)。

Another array is like following: 另一个数组如下所示:

$scope.clickedImages=[2,4,3,1,...]

I am building following function: 我正在建立以下功能:

$scope.checkCrossCorrectness = function(array1, array2){}

My requirement is: 我的要求是:

If the first element of $scope.blinkingBoxes is 2 (or basically any from 1, 2, 3, 4) then in $scope.clickedImages first element can not be 2 (or same as first element of first array), instead could be 1, 3, or 4. This logic continues for further elements as well (ie in first array at second position if 3 comes then in second array second position can be occupied by either 1, 2 or 4) 如果$scope.blinkingBoxes的第一个元素为2(或基本上是$scope.blinkingBoxes中的任何一个),则在$scope.clickedImages第一个元素不能为2(或与第一个数组的第一个元素相同),而是1、3或4。此逻辑也将继续用于其他元素(即,如果3出现在第一个数组中的第二位置,则第二个数组中的第二个位置可以被1、2或4占用)

How can I implement this? 我该如何实施?

I dont really know if this has anything to do with angular specifically, but from what I can tell a simple forEach loop will do to check equality between the indexes. 我真的不知道这是否与angular具体有关,但是据我所知,一个简单的forEach循环将检查索引之间的相等性。

Example: 例:

$scope.blinkedBoxes = [1, 2 ..] // etc
$scope.clickedImages = [2, 1, ..] // etc

function functionToRunOnClickOrWhatever(){
    $scope.blinkedBoxes.forEach(function(val, index){
        var isEqual = val === $scope.clickedImages[index];

        if(isEqual){
            // do something?
        }
    });
}

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

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