1

I have this json object which has seq 1,2,3,4. And in the main HTML page, user can edit the seq number and I need to make sure that every seq is unique and must not repeat like 2 set of "1" & "1". Unfortunately, it is buggy and sometimes it works sometimes not. Can someone point out my mistake? I want to ignore blank value as well

$scope.myDataset.map(v => v.seq).sort().sort((a, b) => {
    if (a !== '' && b !== '' && a === b) {
        $scope.duplicateFound = true
    }
})

$scope.myDataset = [
    { seq: '1', },
    { seq: '2', },
    { seq: '3', },
    { seq: '4', },
    { seq: '', },
    { seq: '', },

];
user3004237
  • 13
  • 1
  • 2
  • 7

6 Answers6

0

Following code may be helpful for you:

$scope.myDataset = [
    { seq: '1', },
    { seq: '2', },
    { seq: '3', },
    { seq: '4', },
];
var dataSeq = $scope.myDataset.map(v => v.seq);
dataSeq.forEach(function (item, index) {
  if (item !== '' && dataSeq.indexOf(item) !== index ) {
        $scope.duplicateFound = true;
        break;
    }
 })
Kermit
  • 1,062
  • 6
  • 10
0

Here is a solution using Set and array#some

var myDataset = [
    { seq: '1', },
    { seq: '2', },
    { seq: '3', },
    { seq: '4', },
];

var visited = new Set();
var hasDuplicates = myDataset.some(function(obj) {
    return visited.size === visited.add(obj.seq).size;
});

console.log(Array.from(visited));
console.log(hasDuplicates);
Hassan Imam
  • 21,956
  • 5
  • 41
  • 51
0

You could use an object as hash table and exclude empty strings.

function hasDupes(array) {
    var hash = Object.create(null);
    return array.some(function (a) {
        return a.seq && (hash[a.seq] || !(hash[a.seq] = true));
    });
}

// false
console.log(hasDupes([{ seq: '4' }, { seq: '1' }, { seq: '2' }, { seq: '7' }, { seq: '6' }, { seq: '5' }]));

// false
console.log(hasDupes([{ seq: '' }, { seq: '1' }, { seq: '' }, { seq: '7' }, { seq: '6' }, { seq: '5' }]));

// true
console.log(hasDupes([{ seq: '4' }, { seq: '1' }, { seq: '2' }, { seq: '4' }, { seq: '3' }, { seq: '4' }]));
Nina Scholz
  • 376,160
  • 25
  • 347
  • 392
0

My guess is you are somehow mixing Strings and integers, which is why your sort is going wrong. I suggest you modify your mapping function a bit.

$scope.myDataset.map(v => parseInt(v.seq + '') ).sort().sort((a, b) => {
    if (a !== '' && b !== '' && a === b) {
        $scope.duplicateFound = true
    }
})
dev8080
  • 3,950
  • 1
  • 12
  • 18
0

I moved the data object above the iteration. Not sure why you were sorting, what the goal was. I added an extra key value pair to the object that you already have to track if it is visited.

In the loop we add another iteration to compare the next element to the current element of the index. To make sure it doesn't go out of the scope of the array we subtract one from the length and when starting the loop we do 'less than' (<), rather than 'less than or equal'(<=) as this would also cause it to go out of the index range of the array.

The visited key will only be true if it was compared to another iteration after itself.

$scope.duplicateFound = false;
$scope.myDataset = [
            {
                seq: '1',
                visited: false
            },
            {
                seq: '2',
                visited: false
            },
            {
                seq: '3',
                visited: false
            },
            {
                seq: '4',
                visited: false
            },
            {
                seq: '4',
                visited: false,
            }
        ];

        for (var index = 0; index < $scope.myDataset.length - 1; index++){
           var index2 = index + 1;

           if ($scope.myDataset[index]['visited'] === false && $scope.myDataset[index2]['visited'] === false) {
            if ($scope.myDataset[index]['seq'] == $scope.myDataset[index2]['seq']) {
              $scope.duplicateFound = true;
            }
           }

          $scope.myDataset[index]['visited'] = true;
        }

        console.log($scope)
user3738936
  • 936
  • 8
  • 22
0

if you like to use Lodash (https://lodash.com/docs/4.17.4) JavaScript library then here here is a good solution.

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

app.controller('testctrl', function($scope) {

$scope.myDataset = [
{ seq: '1', },
{ seq: '2', },
{ seq: '3', },
{ seq: '4', },
{ seq: '2', },
{ seq: '', },
];

console.log($scope.myDataset);

console.log(_.unionBy(_.filter($scope.myDataset, function(o) {
  return !_.isEmpty(o.seq);
}), 'seq'));

});

sahed moral
  • 345
  • 3
  • 13