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?
indexOf()doesn't take an array.indexOflooks for equality, so only primitives will really work like that. ie:"a" === "a" // true, but[] === [] // false. Try looking at this questionz.sort()?players.forEach((v,i,a) => { while(++i < a.length) matches.push([v, a[i]]) })orArray.from(new Set(players)).forEach(...)if players itself may contain duplicates.