vlatko606
vlatko606

Reputation: 1149

javaScript - How to assign multiple dynamic values to a variables into the loop

I have dynamic array of matches as:

[
      "Rivers United - Kano Pillars",
      "MFM FC - Abia Warriors",
      "Lobi Stars - Enugu Rangers",
      "Sunshine Stars - Dakkada",
      "Ifeanyi Ubah - Kwara United",
      "Enyimba - Adamawa United",
      "Empoli - Torino U19",
      "Bologna U19 - Ascoli",
      "Lazio - Inter U19",
      "Juventus U19 - Spal",
      "Milan - Fiorentina U19",
      "Sassuolo U19 - AS Roma U19",
      "Genclerbirligi - Denizlispor",
      "Goztepe - Fenerbahce",
      "Kayserispor - Demir Grup Sivasspor",
      "Trabzonspor U19 - Istanbul Basaksehir AS",
      "Fatih Karagumruk - KASIMPASA AS",
      "Portugal - Israel",
      "Wydad AC Casablanca - DHJ ",
      "Rsb Berkane - Athletic Youssoufia"
    ]

When I perform API call using supertest library based on above array, I am able to fetch single opponent and keep into a single variables and my code is:

const response = await api.getRequestAsync("https://sports/api/feeds/prematch/mostpopularsports/en/1/5/6/", `2021-06-06`, "", "");
        const otp = api.jsonPath(response.body, `$[0].AreaMatches[0].Items..ItemName`);
        let opponent1 = otp[0].split(" - ")[0];
        let opponent2 = otp[0].split(" - ")[1];

        let opponent20 = otp[5].split(" - ")[1];
        console.log(opponent1 + " | " + opponent2 + " | " + opponent20);

which produces variables as: opponent1, opponent2...opponent20

and console output as: [0-0] Rivers United | Kano Pillars | Adamawa United

But this creates manual work to assign value to every single variable.

How can I transfer this code to loop logic, so dynamically will have variable for every single opponent: Rivers United, Kano Pillars...Athletic Youssoufia

and variables as: opponent1, opponent2...opponent20

Upvotes: 0

Views: 361

Answers (2)

vlatko606
vlatko606

Reputation: 1149

Hey @Peter Krebs thanks for your efforts, but the solution that i need is:

        let opponentsA = [];
        let opponentsB = [];

        for (var i = 0; i < otp.length; i++) {
            opponentsA[i] = otp[i].split(" - ")[0];
            opponentsB[i] = otp[i].split(" - ")[1];

        }

        console.log(opponentsA[0]); //access the 1st opponent dynamically
        console.log(opponentsA[20]); //access the 20h opponent dynamically

Upvotes: 0

Peter Pointer
Peter Pointer

Reputation: 4162

Well you can use arrays to store multiple values for example, as you are already using. You just have to create these variables yourself.

See JavaScript - Array from Mozilla Web Docs

In your example code you access otp which has the opponents as strings. We can create a loop over otp to fill arrays with the result of your split() call.

Here is a simple example code you can adapt to your liking:

let opponentsLeft = [];
let opponentsRight = [];

for(var i=0; i < otp.length; i++) {
  var split = otp[i].split(" - ");
  
  opponentsLeft.push(split[0]);
  opponentsRight.push(split[1]);
}

In more modern JavaScript you can use forEach():

let opponentsLeft = [];
let opponentsRight = [];

otp.forEach(element => {
  var split = element.split(" - ");
  
  opponentsLeft.push(split[0]);
  opponentsRight.push(split[1]);
});

You can do what you want with the resulting arrays. Again, just modify to your liking.

You can then write a loop over those arrays to do what you wish:

for(var o = 0; o < opponentsLeft.length; o++) {
  console.log('left', opponentsLeft[o]);
  console.log('right', opponentsRight[o]);
}

Upvotes: 2

Related Questions