Currently, I am utilizing an NBA API that allows me to search for players based on their last name. However, a common issue arises as multiple players can share the same last name.
An illustration of the response received from the API when sorting by last names is as follows:
"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"
}
The results are extensive, necessitating my need to achieve two objectives:
Primarily, I aim to retrieve/filter the correct player using both their first and last names. In this extraction process, it remains imperative to retain the entire array of information associated with the matched player.
To simplify, I want to find 'Deyonta Davis', but upon discovery - I require all other relevant details pertaining to that player (such as years in the league, college attended, country of origin, etc.)
I have currently established a method to access the initial result of nested data within this API through the use of the last name inputted; however, the main predicament lies in the likelihood of the initial result not aligning with the desired player.
The objective is to integrate both first and last names in order to prevent inaccurate player pulls.
A snippet representing my current approach towards extracting information based on last name:
// Accessing 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
}
// Deriving Player Information (initial 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;
Your assistance in resolving this matter would be greatly appreciated. Thank you.