Skip to content

Instantly share code, notes, and snippets.

@juliovedovatto
Created December 3, 2016 19:42
Show Gist options
  • Star 4 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save juliovedovatto/f4ac657e5d28e060c791f5ef27b13341 to your computer and use it in GitHub Desktop.
Save juliovedovatto/f4ac657e5d28e060c791f5ef27b13341 to your computer and use it in GitHub Desktop.
Javascript: Remove duplicates of multidimensional array
// for browser using, this code requires javascript 1.7+
var arr = [
{ foo: 'bar', lorem: 'ipsum' },
{ foo: 'bar', lorem: 'ipsum' },
{ foo: 'bar', lorem: 'ipsum dolor sit amet' }
];
arr = arr.map(JSON.stringify).reverse() // convert to JSON string the array content, then reverse it (to check from end to begining)
.filter(function(item, index, arr){ return arr.indexOf(item, index + 1) === -1; }) // check if there is any occurence of the item in whole array
.reverse().map(JSON.parse) // revert it to original state
console.log(arr); // [ { foo: 'bar', lorem: 'ipsum' }, { foo: 'bar', lorem: 'ipsum dolor sit amet' } ]
@jack-fdrv
Copy link

Works. thanks bro

@maxizhukov
Copy link

Not working for TypeScript at all

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment