1

I am working with javascript arrays, where i have an array of arrays like,

var arr = [ 
  [ 73.0191641, 33.5720131 ],
  [ 73.0191641, 33.5720131 ],  
  [ 3.0191641, 33.5720131 ],
  [ 73.0191641, 33.5720131 ],
  [ 73.0191641, 33.5720131 ],
  [ 73.0192222, 33.5778921 ],
  [ 73.0192222, 33.5778921 ]];

There, i needed to remove the duplicate arrays. I have tried this method but it didn't worked for me, may be i am missing or doing something wrong.

var distinctArr = Array.from(new Set(arr));

Although this method works only for array of objects. If someone can help, please do help. Thanks for your time.

Suhail Mumtaz Awan
  • 2,840
  • 6
  • 35
  • 67
  • 3
    Possible duplicate of [Unique values in an array](http://stackoverflow.com/questions/1960473/unique-values-in-an-array) – Ivanka Todorova May 10 '17 at 09:43
  • 1
    Specify your problem because I guess you want to remove **whole rows** of this array of arrays, not separate elements. If so - it's not a dupe. – kind user May 10 '17 at 09:54

2 Answers2

3

Lodash is great lib for doing this little things.

uniqWith with compare isEqual should resolve your problem.

var arr = [ 
  [ 73.0191641, 33.5720131 ],
  [ 73.0191641, 33.5720131 ],  
  [ 3.0191641, 33.5720131 ],
  [ 73.0191641, 33.5720131 ],
  [ 73.0191641, 33.5720131 ],
  [ 73.0192222, 33.5778921 ],
  [ 73.0192222, 33.5778921 ]];

_.uniqWith(arr,_.isEqual)

return -> [Array(2), Array(2), Array(2)]
dasiekjs
  • 76
  • 5
1

You can use following approach.

var arr = [ { person: { amount: [1,1] } }, { person: { amount: [1,1] } }, { person: { amount: [2,1] } }, { person: { amount: [1,2] } }, { person: { amount: [1,2] } }];

   hash = [...new Set(arr.map(v => JSON.stringify(v)))].map(v => JSON.parse(v));
   
   document.write(`<pre>${JSON.stringify(hash, null, 2)}</pre>`);
kind user
  • 32,209
  • 6
  • 49
  • 63