0

I have an array with list of items.Some items are duplicates.I need the way to make those items as unique.I know of using track by $index in ng-repeat of angularjs.But i want to use the array without trackby.as a regular ng-repeat.

Array=[
        { name:'john',salary:'2000'},{ name:'john',salary:'2000'},{ name:'john',salary:'2000'},{ name:'harry',salary:'2000'}]

Is there any way I can do it in controller of the angularjs.I tried adding new field to the array Like this:

for(x in Array){
 Array[x].unique=x;
  }

But it givessame value to all the duplicate items. Any way to resolve this.

Bikash Ramtel
  • 115
  • 1
  • 3
  • 12
  • 1
    Possible duplicate of [How to get unique values in an array](http://stackoverflow.com/questions/11246758/how-to-get-unique-values-in-an-array) – MysterX Apr 29 '16 at 07:41
  • Try to use other variable name other than Array – ajbee Apr 29 '16 at 07:42
  • isn't clear what you mean with `using track by`, that's doesn't solve your problem and, just for being clear, `track by` is awseome for performances improvements... – Hitmands Apr 29 '16 at 07:43
  • There are symbols in JS to assign unique identities to objects. More for symbols please check https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Symbol – Redu Apr 29 '16 at 07:54
  • @PranavCBalan I just used Array to say its Array data. – Bikash Ramtel Apr 29 '16 at 08:24
  • @MysterX The link you provided is to avoid the duplicates. I need all the items to be listed. – Bikash Ramtel Apr 29 '16 at 08:25
  • @Hitmands Yes I can display all the items in ng-repeat with track by. But i only wanted to know why binding an extra field in the controller using for each don't works. – Bikash Ramtel Apr 29 '16 at 08:28
  • Is it accurate to say, then, that you just want to give each item a unique identifier? – Chris Baker Apr 29 '16 at 13:53

1 Answers1

0

You could use lodash's _.uniq()

newArr = _.uniq(arr);
rrd
  • 5,789
  • 3
  • 28
  • 36