const arr = [
['a', 'b', 'c'],
['a', 'd'],
['d', 'e'],
['f', 'g'],
['f', 'a'],
['h', 'g'],
['i']
]
function test(arr) {
const r = []
arr = arr.map(item => item.sort()).sort()
console.log(arr)
arr.forEach(item => {
if (r.length === 0) {
r.push(item)
} else {
const index = r.length - 1;
let before = r[index];
const flag = before.some(b => item.includes(b));
if (flag) {
before = Array.from(new Set([...before, ...item]))
r.splice(index, 1, before)
} else {
r.push(item)
}
}
})
return r
}
console.log(test(arr))
console