简体   繁体   English

提取 JSON 中的特定嵌套数组 与 Javascript 匹配数据的对象

[英]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.我正在使用 NBA API,其中一个功能是按姓氏查找球员。 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:使用姓氏排序时来自 API 的响应示例:

   "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.)所以本质上,我想要“Deyonta Davis”,但是当找到时 - 我还需要所述玩家信息的 rest(职业年限、大学、国家等)

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.我已经设置了一个命令来通过姓氏检索此 API 中嵌套数据的第一个结果 - 该命令采用您输入的姓氏并发送第一个结果。 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.从属性firstNamelastName等于所需值的数组中检索第一个匹配的 object。

To achieve this you could use build in find function.为此,您可以使用 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 .请记住,如果数组中没有这样的 object ,则player将是undefined

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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