Nitish Nitish - 1 year ago 83
Javascript Question

Angularjs comparing elements from two arrays

I have one array something like following:


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:


I am building following function:

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

My requirement is:

If the first element of
is 2 (or basically any from 1, 2, 3, 4) then in
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 Source

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.


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

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

            // do something?
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download