Nitish Nitish - 4 months ago 10
Javascript Question

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).

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 (i.e. in first array at second position if 3 comes then in second array second position can be occupied by either 1, 2 or 4)

How can I implement this?

Answer

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.

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