skip to Main Content

2

Answers


  1. I’ve modified the above code assuming that fetchData function will get the data from API. Please ignore the implementation of fetchData.

    const fetchData = async (url) => {
        return await fetch(url).then(res => res.json())
    }
    
    const getPlayerData = async () => {
      const allPlayers = await fetchData("https://api.sleeper.app/v1/players/nfl");
      const players = Object.keys(allPlayers).map((key) => allPlayers[key]);
    
      const activePlayers = players
              ?.filter((player) => player.search_rank !== null) 
              .sort((a, b) => a.search_rank - b.search_rank);
      
      console.log(activePlayers)
      return activePlayers;
    }

    Changes made:

    • Modified line which contains const players to use arrow function and return it without explicitly writing return statement. — You may ignore this change.
    • Removed unnecessary Promise.all as the data is already received and we are applying filter and sort functions which are synchronous operations. async is also not required inside the filter callback as we are not performing any asynchronous operations inside filter function.

    The main issue is we have to return player.search_rank !== null which is present inside filter function or write it as arrow function without brackets {}.

    Login or Signup to reply.
  2. Friend, I did some major changes to your function and now it works just fine.

    const fetchData = async ( url ) => await (await fetch(url)).json();
    
    const getPlayerData = async () =>
    {
      const players = Object.values
      (
        await fetchData("https://api.sleeper.app/v1/players/nfl")
      );
      
      const rankedPlayers = players
        .filter
        (
          player => ( player.active && (player.search_rank != null) )
        )
        .sort
        (
          ( first, second ) =>
          (
            ( first.search_rank == second.search_rank )
            ? 0 // stays in place
            : ( first.search_rank > second.search_rank )
              ? +1 // first goes after second
              : -1 // first goes before second
          )
        );
      
      console.log
      ({
        'Ranked Players': rankedPlayers.length,
        'First Ranked Player': rankedPlayers.at(0),
      });
      
      return rankedPlayers;
    }
    
    console.clear();
    console.log('Fetching data...');
    getPlayerData();
    .as-console-wrapper { min-height: 100%; }

    By the way, it’s possible to make it a one-liner:

    // One-liner:
    const getPlayerData = async () => Object.values(await (await fetch("https://api.sleeper.app/v1/players/nfl")).json()).filter(player => ( player.active && (player.search_rank != null) )).sort(( first, second ) => (( first.search_rank == second.search_rank )? 0 : ( first.search_rank > second.search_rank )? +1 : -1));
    
    // TEST:
    console.clear();
    console.log('Fetching data...');
    
    getPlayerData().then
    ( rankedPlayers => console.log('First Player:', rankedPlayers.at(0)) );
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search