简体   繁体   中英

Extract specific nested array in JSON Objects that match data with Javascript

I'm working with an NBA API where one of the features is finding players by their last name. The issue I have; is that multiple players can have the same last name, of course.

An example of the response from the API when sorting with last names:

   "players": [
    0: {
    "firstName":"Anthony"
    "lastName":"Davis"
    "teamId":"17"
    "yearsPro":"9"
    "collegeName":"Kentucky"
    "country":"USA"
    "playerId":"126"
    "dateOfBirth":"1993-03-11"
    "affiliation":"Kentucky/USA"
    "startNba":"2012"
    "heightInMeters":"2.08"
    "weightInKilograms":"114.8"
    
    1: {
    "firstName":"Deyonta"
    "lastName":"Davis"
    "teamId":"14"
    "yearsPro":"3"
    "collegeName":"Michigan State"
    "country":"USA"
    "playerId":"127"
    "dateOfBirth":"1996-12-02"
    "affiliation":"Michigan State/USA"
    "startNba":"2016"
    "heightInMeters":"2.11"
    "weightInKilograms":"107.5"
}

I limited the results here, but it goes on and on, etc. So, I am looking to do two things:

First, extract/filter the correct player using their first name and last name. In said extraction, I still need the complete array information when it is matched.

So essentially, I want 'Deyonta Davis', but when found - I also need the rest of said player's information (years pro, college, country, etc.)

I already have a command set up to retrieve the first result of the nested data in this API via last name - where the command takes the last name you input and sends the first result. The precise problem is that the first result is likely not to be the guy you are looking for.

The goal is to include first & last name to avoid pulling the wrong player.

A snippet of how I currently call the information via last name:

// Calling API
    const splitmsg = message.content.split(' ')
    var lastnameurl = "https://api-nba-v1.p.rapidapi.com/players/lastName/" + splitmsg[1];
    axios.get(lastnameurl, {
      headers: {
      "x-rapidapi-key": apikey,
      "x-rapidapi-host": apihost
    }

// Extracting Player Information (first result)
var playerfirstname = response.data.api.players[0].firstName;
var playerlastname = response.data.api.players[0].lastName;
var collegename = response.data.api.players[0].collegeName;
var countryname = response.data.api.players[0].country;
var playerDOB = response.data.api.players[0].dateOfBirth;
var yrspro = response.data.api.players[0].yearsPro;
var startednba = response.data.api.players[0].startNba;

Any help would be appreciated, thank you.

If I understand the question correctly the task is:

Retrieve first matching object from an array where properties firstName and lastName equal to desired values.

To achieve this you could use build in find function.

const player = array.find(el => {
  return el.firstName === "Deyonta" && el.lastName === "Davis"
});

Keep in mind if there is no such object in array the player will be undefined .

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