I have a 2D array matches
where I store the game id and another array of objects games
where I store gameId
with the playerId
.
Here is what I have done so far:
Step 1: I get the opponent's gameId against the gameId from "matches" array.
e.g. gameId "2" has an opponent "1"
Step 2: check opponent_game in games and return game.
let gameId = 2
let matches = [
[1, 2],
[3, 4]
]
const opponent_game = matches.filter((v, i) => {
if (v.includes(gameId)) {
var index = v.indexOf(gameId);
if (index > -1) {
return v.splice(index, 1);
}
}
})
let games = [{
gameId: 1,
playerId: 222
}, {
gameId: 4,
playerId: 222
}]
const game = games.filter((v, i) => {
return v.gameId === opponent_game[0][0]
})
console.log(game)
Please suggest best practices in javascript or an optimized and clean way to write the same solution. TIA
matches.filter
call looks wrong. The callback function should return a boolean, but you are returning an array orundefined
. \$\endgroup\$