0

I am trying to filter a table based on the user's input (search). This search string sends a teamId to the getFilteredPlayers function and I would like to return a list of players that are linked to that teamId. See an example of a player in the players array below;

enter image description here

See getFilteredPlayers function below, it is not returning anything currently. Please recommend some changes please

let getFilteredPlayers = (players, search) => {
            if (search && search.length > 0) {
                return players.filter((x) => {
                    return search.any((y) => x.playersTeams == y);
                });
            }
            return players;
        };
Ciaran
  • 13
  • 3
  • `players.filter(p => p.playersTeams.some(t => t.teamId === search))` – adiga Dec 21 '22 at 13:59
  • thanks for this, where do i put it? – Ciaran Dec 21 '22 at 14:02
  • inside the `if` block. Replace `players.filter(...)` with the above filter code – adiga Dec 21 '22 at 14:10
  • let getFilteredPlayers = (players, search) => { if (search && search.length > 0) { return players.filter(p => p.playersTeams.some(t => t.teamId === search)) }; return players; }; it is not working like this – Ciaran Dec 21 '22 at 14:25
  • What is the value in `search`? This works only if `search` has the entire `teamId` string. You have mentioned that *"This search string sends a teamId*" – adiga Dec 21 '22 at 14:26
  • Search is an array that contains teamId(s) ; ['08da89a7-bc69-489e-8643-0a8290d2c6bf'] – Ciaran Dec 21 '22 at 14:28
  • Then you need something like this: `players.filter(p => p.playersTeams.some(t => search.includes(t.teamId) ))` – adiga Dec 21 '22 at 14:29

0 Answers0