2

I want to check if there exists duplicate outputTypeId in the output array object..

Below is the JSON:

 $scope.entities=  [
        {
            "input": {
                "id": 134
            },
            "output": [
                {
                    "id": 135,
                    "outputTypeId": 4
                }
            ]
        },
        {
            "input": {
                "id": 134
            },
            "output": [
                {
                    "id": 135,
                    "outputTypeId": 7
                }
            ]
        },
        {
            "input": {
                "id": 134
            },
            "output": [
                {
                    "id": 135,
                    "outputTypeId": 9
                }
            ]
        }
    ]

Below is the code that I tried but its not going inside the condition after execution..

Let outputTypeId be [7] as I'm checking for multiple outputTypeId's,hence an array

   $scope.checkForDuplicateOutputs = (outputTypeId) => {
        for (var i = 0; i < $scope.entities.length; i++) {
            for (var j = i; j < $scope.entities[i].output[j].length; j++) {

                if (outputTypeId.contains($scope.entities[i].output[j].outputTypeId)) {
                    $scope.isDuplicateOutput = true;
                    break;
                } else {
                    $scope.isDuplicateOutput = false;

                }
            }
        }
    }
0

2 Answers 2

1
 function checkForDuplicates(outputTypeIds) {
       $scope.isDuplicateOutput = $scope.entities.some(function(entity) { // Loop through entities
           return entity.output.some(function(entityOutput) { // Look for any output
              return outputTypeIds.indexOf(entityOutput.outputTypeId) != -1; // which is duplicated in 'outputTypeIds'
           });
       });
    }

So this solution uses Array.some - It has a few advantages:

  • Removes the need to manually break your loops
  • No need to have i and j variables to keep track of loop counters
  • No need to duplicate $scope.isDuplicateOutput = <boolean>;
  • Less lines of code :)
Sign up to request clarification or add additional context in comments.

Comments

0

You are breaking only the inner loop with that break statement and the problem is even though the duplicate flag does get set to true, it will be reset to false in the next iteration. Basically, in the end you'll get the result of the last iteration only.

The quickest fix is to use a flag to denote whether the external loop needs to be stopped:

$scope.checkForDuplicateOutputs = (outputTypeId) => {
    var breakOut = false;                 // <--- this is new
    for (var i = 0; i < $scope.entities.length; i++) {
        if (breakOut) break;              // <--- this is new
        for (var j = i; j < $scope.entities[i].output[j].length; j++) 
            if (outputTypeId.contains($scope.entities[i].output[j].outputTypeId)) {
                $scope.isDuplicateOutput = true;
                breakOut = true;          // <--- this is new
                break;
            } else {
                $scope.isDuplicateOutput = false;
            }
        }
    }
}

If you still want to iterate all the entities and have a list of all the duplicates, you can make $scope.isDuplicateOutput an array and just push the duplicate ids into it.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.