Unique array of arrays in JavaScript

Let's assume that you have the following array:

javascript
let arr = [
    ['Erik', 'Jeff', 'Elon'],
    ['Bill', 'Elon', 'Jeff'],
    ['Bill', 'Erik', 'Jeff'],
    ['Erik', 'Jeff', 'Elon'],
    ['Bill', 'Erik', 'Jeff']
]

/* Unique array of arrays: 
res = [
    ['Erik', 'Jeff', 'Elon'],
    ['Bill', 'Elon', 'Jeff'],
    ['Bill', 'Erik', 'Jeff'],
]*/

As keys are unique in objects, you can use an empty object and add the arrays as keys. Then, you can use the Object.keys() function to get back the keys:

javascript
let obj = {}

arr.forEach(e => obj[JSON.stringify(e)] = true) 
/* obj = {
    ["Erik","Jeff","Elon"]: true, 
    ["Bill","Elon","Jeff"]: true, 
    ["Bill","Erik","Jeff"]: true
} 
*/

let res = Object.keys(obj).map(JSON.parse) 
/*
res = [
    ['Erik', 'Jeff', 'Elon']
    ['Bill', 'Elon', 'Jeff']
    ['Bill', 'Erik', 'Jeff']
]
*/

Hi, I'm Erik, an engineer from Barcelona.

If you like the post, say hi or join Substack to get non-frequent updates.