'Can I set a limit on the forEach command? [TypeScript]

With the forEach command, I pull the team players in the lobby in my web-based game. For a feature in my game, I want to get the id of the first player in the opposing team. How can I do this over my existing code?

The code below is getting the id of the list of all the players in the opposing team. I just want to get the id of the first player in the opposing team. I hope I was able to explain what I mean.

lobby.players
    .filter((player) => player.team !== team)
    .forEach((player) => {
        socketServer.to(player.id).emit('GameRound', Role.Watcher);
    });


Solution 1:[1]

you don't need a loop here if you just want to get the id of the first player, check the example below:

const player = lobby.players.filter((player) => player.team !== team)
socketServer.to(player[0].id).emit('GameRound', Role.Watcher);

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Zain Khan