How to get distinct values from an array of arrays in JavaScript using the filter() method?

arrays, javascript

Solution

Try converting the inner arrays to a string, then filter the dupes and parse the string again.

let x = [[1, 2], [3, 4], [1, 2]];

var unique = x.map(ar=>JSON.stringify(ar))
  .filter((itm, idx, arr) => arr.indexOf(itm) === idx)
  .map(str=>JSON.parse(str));

console.log(unique);

Problem

I have an array like this: ``` let x = [[1, 2], [3, 4], [1, 2], [2, 1]]; ``` What should I do to retrieve an array without the duplicates? ``` [[1, 2], [3, 4], [2, 1]]; ``` I would like to use the filter method. I tried this but it doesn't work: ``` x.filter((value,index,self) => (self.indexOf(value) === index)) ``` EDIT: as I specified to use the filter method, I don't think this question is a duplicate. Also, I got several interesting answers.

Original source

Related problems