0

I have this type of JSON data

enter image description here

I have written the code to eliminate duplicate records in JQuery

function GetUniqueTablesCategory(Data) {
  var UniqueNames = $.unique(Data, function(d) {
    return d.TableName;
  });
  return UniqueNames;
}

But I am getting this error below, can anyone please help me out to eliminate this issue.

enter image description here

Shaunak D
  • 20,588
  • 10
  • 46
  • 79
G R
  • 137
  • 1
  • 3
  • 12
  • 1
    It throws an error because objects cannot be sorted. The `$.unique()` method is designed to be used with an array of DOMElements, see https://api.jquery.com/jQuery.unique/ – Rory McCrossan May 13 '15 at 07:31
  • Is this while running your code? It seems that you do not give an array as an argument. Did you convert the JSON into an array? – Sylvia Stuurman May 13 '15 at 07:31
  • Rory McCrossan , how I will achieve this without using $.unique() method, can you please share me any link. – G R May 13 '15 at 07:34
  • 1
    You need to write your own logic to replicate it, via loops. – Rory McCrossan May 13 '15 at 07:39
  • I am going to write my own logic . , thanks . once I done will add in post . – G R May 13 '15 at 07:43

4 Answers4

3

You can write a one-liner using Set from ES6

let uniqueNames = [...new Set(Data.map(d => d.TableName))]
Hunter
  • 3,080
  • 20
  • 23
1

You can do that with a simple loop :

function GetUniqueTablesCategory(Data) {
  var UniqueNames = [];
  Data.forEach(function(value) {
      if (UniqueNames.indexOf(value.TableName) === -1) {
          UniqueNames.push(value.TableName);
      }
  });

  return UniqueNames ;
}
jmgross
  • 2,306
  • 1
  • 20
  • 24
1

If underscore.js is acceptable, you can write your method like this:

function GetUniqueTablesCategory(data) {
  return _.uniq(_.map(data, function(d){return d.Name}));
}

fiddle

huan feng
  • 7,307
  • 2
  • 32
  • 56
1

try something like this:

$(document).ready(function() {
 var lookup = {};
 var obj = [{'TableName': 'prop1','value5': 'prop3'},{ 'TableName': 'prop1','value5': 'prop3'}, {'TableName': 'prop3','value5': 'prop3'}];
 var array=[];
 for (var ob, i = 0; ob = obj[i++];) {
    var name = ob.TableName;

    if (!(name in lookup)) {
      lookup[name] = 1;
      array.push(name);
    }
  }

 alert(JSON.stringify(array));
 
 });//submit click 
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

you can find a similar thread here also.see

Community
  • 1
  • 1
Suchit kumar
  • 11,809
  • 3
  • 22
  • 44