Validate a table of unique ids

by jacobwsmith

HTML

<script src="//ajax.googleapis.com/ajax/libs/angularjs/1.2.0/angular.min.js"></script>
<h1>Validate unique ids</h1>

<div ng-controller="MyCtrl">
    <table>
        <thead>
            <tr>
                <td>Id</td>
            </tr>
        </thead>
        <tbody>
            <tr ng-repeat="item in data" ng-class="{highlight: item.pass == false}">
                <td>{{item .id}}</td>
            </tr>
        </tbody>
    </table>
</div>

CSS

.highlight {
    background-color: yellow;
}

JavaScript

var app = angular.module('myApp', []);

function MyCtrl($scope) {

    $scope.data = [{
        id: '5'
    }, {
        id: '2'
    }, {
        id: '3'
    }, {
        id: '4'
    }, {
        id: '5'
    }, {
        id: '6'
    }, {
        id: '7'
    }, {
        id: '8'
    }, {
        id: '2'
    }];
    
    // validate temp object
    var temp = {}; 
    for(var i=0;i<$scope.data.length; i++){
        
        if(temp[$scope.data[i].id] !== undefined){
            // fail
            $scope.data[i].pass = false;
            $scope.data[temp[$scope.data[i].id]].pass = false;
        }else{
            // pass
            temp[$scope.data[i].id] = i;
        }
         
    }
}