Home > Blockchain >  Compare data in API to known variable , find matching item
Compare data in API to known variable , find matching item

Time:08-12

I have a global variable defined that will matchup to a "league_id" in the API.

I'm making ajax calls to post some forms and I don't know the correct server at the time , so i need to check the API , use the league_id that is know and get the corresponding url server.....width is the 2 digits after //www in each url path in the API.

Here is my ajax call , i have fiddled with this for hours and can't figure it out

var knownID = "32537";

var url = '//api.myfantasyleague.com/'   year   '/export?TYPE=myleagues&JSON=1';
function GetServerID() {
    $.ajax({
        url: url,
        type: "GET",
        dataType: 'json',
        xhrFields: {
            withCredentials: true
        },
        success: function (data) {
            myLeagues = data.leagues.league;
            // get the serverID that matched the "knownID" which is 32537 in this example
            // the server id for this league is "46" - the 2 digits after "www" in the API url item
            // set new variable that matches the serverID to the global variable "knowID"
        }
    });
}

API data

{"leagues":{"league":[
{"franchise_id":"0001","url":"https://www46.myfantasyleague.com/2022/home/32537","name":"Auction Draft","league_id":"32537"},
{"franchise_id":"0001","url":"https://www49.myfantasyleague.com/2022/home/41681","name":"Contest League Test","league_id":"41681"},
{"franchise_id":"0001","url":"https://www46.myfantasyleague.com/2022/home/43619","name":"Draft","league_id":"43619"},
{"franchise_id":"0000","url":"https://www48.myfantasyleague.com/2022/home/19048","name":"MFL Manager","league_id":"19048"}]
}}

CodePudding user response:

You want to find an Object element based on a specific value of the element. This is a pretty common search.

Consider the following examples: https://www.tutorialrepublic.com/faq/how-to-find-an-object-by-property-value-in-an-array-of-javascript-objects.php

The find() method returns the first element in the provided array that satisfies the provided testing function. If no values satisfy the testing function, undefined is returned.

$(function() {
  var responseData = {
    "leagues": {
      "league": [{
          "franchise_id": "0001",
          "url": "https://www46.myfantasyleague.com/2022/home/32537",
          "name": "Auction Draft",
          "league_id": "32537"
        },
        {
          "franchise_id": "0001",
          "url": "https://www49.myfantasyleague.com/2022/home/41681",
          "name": "Contest League Test",
          "league_id": "41681"
        },
        {
          "franchise_id": "0001",
          "url": "https://www46.myfantasyleague.com/2022/home/43619",
          "name": "Draft",
          "league_id": "43619"
        },
        {
          "franchise_id": "0000",
          "url": "https://www48.myfantasyleague.com/2022/home/19048",
          "name": "MFL Manager",
          "league_id": "19048"
        }
      ]
    }
  };

  var knownID = "32537";

  var results = responseData.leagues.league.find(l => l.league_id == knownID);

  console.log(results);

});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>

  • Related