I'm trying to combine arrays of string with unique combinations, like 'Mark|Tom' but without 'Tom|Mark'
I have written this code:
let arr = ['Tom', 'Danny', 'Mark']
let sets = []
for (let i = 0; i < arr.length; i++) {
let others = arr.filter(name => name != arr[i])
others.forEach((other) => {
let newel = arr[i] + '|' + other
let test = newel.split('|')
if (sets.includes(test[1] + '|' + test[0]) || sets.includes(newel)) return
sets.push(newel)
})
}
console.log(sets)
This is iterating through every array element and then creating another array of other elements from basic array and then iterating through them (again iteration), creating a combination, checking if there is reversed combination of our elements (if this was created in previous loops) and if there is no such combination = push it to target array.
Is there more elegant way to do that task?
Mark|TomnotTom|Mark?Tony|Dany|Markalso to be in output ?