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.

2
  • 3
    Possible duplicate of Unique values in an array Commented May 10, 2017 at 9: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. Commented May 10, 2017 at 9:54

2 Answers 2

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)]
Sign up to request clarification or add additional context in comments.

Comments

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>`);

3 Comments

Thanks for the help, it really worked. any idea what should be done for the array of kind var array = [ { person: { amount: [1,1] } }, { person: { amount: [1,1] } }, { person: { amount: [2,1] } }, { person: { amount: [1,2] } }, { person: { amount: [1,2] } }]; ??
@SuhailMumtazAwan Do you still have problem with this?
No, not at all.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.