I have a simple script that makes a fixture matching all values of an array against all.
const players = ['a', 'b', 'c', 'd'];
const matches = [];
players.forEach(k => {
players.forEach(j => {
if(k !== j) {
let z = [k, j]
z.sort()
const inArray = matches.indexOf(z) !== -1
if(!inArray) matches.push(z)
console.log(matches)
}
})
})
Although asking Javascript search for if z
is in matches
array, the result has duplicated items and returns this:
[ 'a', 'b' ]
[ 'a', 'c' ]
[ 'a', 'd' ]
[ 'a', 'b' ] --> duplicated item
[ 'b', 'c' ]
[ 'b', 'd' ]
[ 'a', 'c' ] --> duplicated item
[ 'b', 'c' ] --> duplicated item
[ 'c', 'd' ]
[ 'a', 'd' ] --> duplicated item
[ 'b', 'd' ] --> duplicated item
[ 'c', 'd' ] --> duplicated item
How can avoid these duplicated items?