简体   繁体   中英

Use variable to find json object

I'm trying to use a variable to match an id inside an array

There are 10 arrays under players and I want to cycle through all of them to see if my variable matches the id. If it does, It should use that array to and show the objects.

async function showMatch() {
// My variable
let userid = 71471603
let Response = await fetch(`https://api.myjson.com/bins/1e96uo`);
let matchlist = await Response.json();

}
showMatch(); 

So it should go cycle each matchlist.players[0 to 9].id , to see if it matches my userid variable.

Consider this example:

const animals = [
    {
        "name": "cat",
        "size": "small",
        "weight": 5
    },
    {
        "name": "dog",
        "size": "small",
        "weight": 10
    },
    {
        "name": "lion",
        "size": "medium",
        "weight": 150
    },
    {
        "name": "elephant",
        "size": "big",
        "weight": 5000
    }
];

let filterArray = animals.filter((animal) => {return animal.size === 'small'});
console.log(filterArray);// Returns an array which satisfies the match

Use .filter to filter out the values you are looking for from an array.

This should do the trick:

async function showMatch() {
  const userid = 71471603
  const Response = await fetch(`https://api.myjson.com/bins/1e96uo`);
  const matchlist = await Response.json();

  return matchlist.players.filter(player => players.id === userid)
}

Short answer: console because you can't return from a promise function (except using then)

 console.log(matchList.players.filter(player => player.id === userid)[0])

Long answer: if you execute this code in your console it will console the information for:

1- EspartaN

2- Elsa

async function showMatch(idOfSpecificUser) {
  // idOfSpecificUser: the id of the user you want his/her information
  // make the GET request
  let response = await fetch(`https://api.myjson.com/bins/1e96uo`);
  // convert the response to JSON
  let responseToJson = await response.json();
  // take the players array from the response after convert it to JSON
  let playersArrayOfObject = responseToJson.players;
  // console.log(playersArrayOfObject); // => [ {}, {}, {}, ... ]

  // now the real fun start
  // you need to iterate over this array and get the user data
  // why [0] cause filter return to you array of object (we need the first match)
  // or the only one match // => [ {id: 71471603, username: "EspartaN", .....} ]
  let userInfo = playersArrayOfObject.filter(
    user => user.id === idOfSpecificUser
  )[0];


  console.log(userInfo.username); //=> "EspartaN"
  console.log(userInfo); // => { id: 71471603, username: "EspartaN", ..... }
}

// => "EspartaN"
showMatch(71471603);

// => "Elsa"
showMatch(97531);

If you need any explanation, or this is not what you are asking about, please comment on my answer

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM