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.
asked May 10, 2017 at 9:42
Suhail Mumtaz Awan
3,5038 gold badges48 silver badges77 bronze badges
-
3Possible duplicate of Unique values in an arrayIvanka Todorova– Ivanka Todorova2017年05月10日 09:43:18 +00:00Commented May 10, 2017 at 9:43
-
1Specify 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– kind user2017年05月10日 09:54:24 +00:00Commented May 10, 2017 at 9:54
2 Answers 2
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
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>`);
answered May 10, 2017 at 9:46
kind user
42k8 gold badges69 silver badges78 bronze badges
3 Comments
Suhail Mumtaz Awan
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] } }]; ??
kind user
@SuhailMumtazAwan Do you still have problem with this?
Suhail Mumtaz Awan
No, not at all.
lang-js