0

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"}]
}}
MShack
  • 642
  • 1
  • 14
  • 33
  • Does this answer your question? [Find object by id in an array of JavaScript objects](https://stackoverflow.com/questions/7364150/find-object-by-id-in-an-array-of-javascript-objects) – Heretic Monkey Aug 11 '22 at 22:12
  • That’s confuses me even more – MShack Aug 11 '22 at 22:18
  • `myLeagues` is an array of objects, according to the example given here. Applying the answers in that duplicate, you should be able to do `myLeagues.find(x => x.league_id === knownID)` and it will get you the object. The rest is getting substrings from strings. A quick search of Stack Overflow yields [Get Substring between two characters using javascript](https://stackoverflow.com/q/14867835/215552) to answer that. – Heretic Monkey Aug 11 '22 at 22:25

1 Answers1

1

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>
Twisty
  • 30,304
  • 2
  • 26
  • 45