0

I'm trying to get all users from api and I need to find the user which most get paid.

so for example

let users=['tom','jenny','smith','Joe']

async function getUsers() { 
  let response = await fetch(`http://something.something?q=${users}`);
  let data = await response.json();
  return data;
}

getUsers().then(data =>console.log(data))

so my plan is users[0],users[1] something like a make function which I add index number via loop.

and get all users and find out who get the most paid.

so my question is how can do fetch users step by step.

Evan Bechtol
  • 2,855
  • 2
  • 18
  • 36
Marc
  • 1
  • 2

2 Answers2

1

You can use Array reduce to get the user who less payed and combine with Promise.all using async / await to fetch all user data

'use strict';

/* mock data for testing purposes
const usersData = [{
  name: 'john doe',
  payed: 10,
}, {
  name: 'john doe01',
  payed: 5,
}, {
  name: 'john doe02',
  payed: 8,
}, {
  name: 'john doe03',
  payed: 20,
}, {
  name: 'john doe04',
  payed: 40,
}, {
  name: 'john doe05',
  payed: 37,
}];
*/

async function getUsers() {
  const usersResponse = await Promise.all(
    usersName.map(userName => fetch(`http://something.something?q=${userName}`))
  );

  return usersResponse.map(userResponse => userResponse.json());
}

async function init() {
  try {
    const usersName = [
      'tom',
      'jenny',
      'smith',
      'Joe',
    ];

    const usersData = await getUsers(usersName);

    const userWhoLessPayed = usersData.reduce((prevUser, currentUser) => {
      if (prevUser.payed > currentUser.payed) {
        return currentUser;
      }

      return prevUser;
    });
    console.log(userWhoLessPayed);
  } catch (e) {
    console.error(e);
  }
}

init();
Jose Mato
  • 2,709
  • 1
  • 17
  • 18
0

Maybe try something like this? Do you need to use async?

Basically, the strategy is this:

  1. Return all users. Unless the endpoint has a way to filter (via parameters - in some way where you currently cannot ascertain the highest paid artist)
  2. Once returned, take that list of users returned from the endpoint and filter. Below, we're filtering a returned array of objects, looking for the highest value of a fictional key called 'pay'.
    $.when( getAllArtists() ).then(function (allArtistResults) {
        $.each(allArtistResults, function(key, value){
             // let's just pretend we get an array of objects back
             // you could just filter it here?
             // props on below: https://stackoverflow.com/questions/4020796/finding-the-max-value-of-an-attribute-in-an-array-of-objects
             Math.max.apply(Math, value.map(function(artists) { return artists.pay; }))
        }
    }
    function getAllArtists() {
        return $.ajax({
            url: 'your-endpoint-here',
            type: 'GET'
        });
    }
markp
  • 74
  • 1
  • 3
  • Okay. I may have misunderstood the first line: "I'm trying to get all users from api.....". Could you please let us know? – markp Jul 02 '19 at 18:54
  • yes, the endpoint only works when I put name of the user. so I make the array myself. well that one was just example .. so I need to use promise or async await to do that... :( – Marc Jul 02 '19 at 18:57
  • Okay, so you have a pre-defined array of users you need to add data to from the endpoint, correct? So, you start here: `users['tom','jenny','smith','Joe']` and you end up with something like `modifiedUsers[ [0]:{tom object}, [1]:{jenny object},[2]{smith object}, ....` – markp Jul 02 '19 at 19:42