const arr = [[1, 2], [1, 2], [3, 4], [7, 7]];
how to remove duplicate arrays from the given array? The first two elements of the array are the same. I couldn't fix it with the set.
const arr = [[1, 2], [1, 2], [3, 4], [7, 7]];
how to remove duplicate arrays from the given array? The first two elements of the array are the same. I couldn't fix it with the set.
Use method filter().
const arr = [[1, 2], [1, 2], [3, 4], [7, 7]];
const arrNoDouble = arr.filter((a = {}, b => !(a[b] = b in a)));
console.log(JSON.stringify(arrNoDouble));
The easiest way to do this:
const arr = [
[1, 2],
[1, 2],
[3, 4],
[7, 7]
];
const newArr = Array.from(new Set(arr.map(JSON.stringify)), JSON.parse)
console.log(newArr)
You could convert the array of arrays into an array of strings with JSON.stringify. This way, the Set's filtering magic can filter the values correctly as they are primitive values.
const arr = [[1, 2], [1, 2], [3, 4], [7, 7]];
let el = arr.map(a => JSON.stringify(a));
let set = new Set(el);
let unique = Array.from(set, JSON.parse);
// one liner
let uniqueArr = Array.from(new Set(arr.map(JSON.stringify)), JSON.parse);
console.log(uniqueArr);
You could use this method to create a new array newarr without duplicates from your current array arr:
const arr = [[1, 2], [1, 2], [3, 4], [7, 7]];
const m = {}
const newarr = []
for (let i=0; i<arr.length; i++) {
const v = arr[i];
if (!m[v]) {
newarr.push(v);
m[v]=true;
}
}
console.log(newarr);