Skip to main content
Braniac
June 6, 2022
Answered

javascript - looping through variables to create new dynamic variables

  • June 6, 2022
  • 1 reply
  • 1990 views

Anyone got an ideas?

 

I have a list of dogs as separate variables, coming from a badly formated API.  Should have put the dogs in an array!

 

dog1 = "Poodle";

dog2 = "Greyhound";

dog3 = "Beagle";

dog4 = "Border Terrier";

dog5 = "Blood Hound";

dog6 = "West Highland Terrier";

dog7 = "Alsatian";

dog8 = "Scooby Doo";

etc...

 

Anyway I want to 'loop' through the dogs:

 

for(let i = 0; i < 20; i++) {

listDogs.innerHTML += `${dog[i]}`;

}

 

Obviously the above doesnt work as its not valid code but you get the idea.

 

I can get this working using 'eval' but am reluctant to use it because of poor write ups.

 

for(let i = 0; i < 20; i++) {

let dogList = eval(`dog${i}`);
listDogs.innerHTML += `<li>${dogList}</li>`

}

 

or I can push to an array, which I can then loop through, but is kind of long-winded and not very elegant, but more secure:

 

let dogList = [];

dogList.push(dog1);

dogList.push(dog2);

dogList.push(dog3);

dogList.push(dog4);

dogList.push(dog5);

dogList.push(dog6);

etc...

 

 

Is there some other solution that I'm obviously missing which creates dynamic variables like eval does (it obviously bolts a string and a variable number together to form a new variable)???

 

Cheers

 

Os

 

 

    This topic has been closed for replies.
    Correct answer B i r n o u

    Below is the basic file structure. There's a lot more data  (I've cut a lot out) but you can see the format:

     

    {

    "dogShow": [
    {
    "showId": "Number",
    "showName": “String”,
    "showCategory": "String",
    "showLocation": “String”,
    "showDetails": “String”,

    "showDog1": “String",
    "showDog2": “String",
    "showDog3": “String",
    "showDog4": “String",
    "showDog5": “String",
    "showDog6": “String",
    "showDog7": “String",
    "showDog8": “String",
    "showDog9": “String",
    "showDog10": "String",

    }
    ]

    }


            let dogs = []
            let result = {
                "dogShow": [
                    {
                        "showId": "Number",
                        "showName": "String",
                        "showCategory": "String",
                        "showLocation": "String",
                        "showDetails": "String",
                        "showDog1": "String",
                        "showDog2": "String",
                        "showDog3": "String",
                        "showDog4": "String",
                        "showDog5": "String",
                        "showDog6": "String",
                        "showDog7": "String",
                        "showDog8": "String",
                        "showDog9": "String",
                        "showDog10": "String",
                    }
                ]
            }
            Object.entries(result.dogShow[0]).forEach(([key, value]) => {
               if ( key.startsWith("showDog") ) dogs.push( value );   
            })
            console.log(dogs)

    1 reply

    Liam Dilley
    Inspiring
    June 7, 2022

    Hey there,

    This is missing a lot of critical bits of information and not very clear 😞


    Where is the content actually coming from?

    You mention API.. What is the output, is it JSON or... What is tha actual data you have to work with - this is crticial.

    There should be zero reason to use eval or most of what your looking to do as it does not make much sense in if your pulling data and rendering it on a web page.

    Happy to help but could you provide a bit more informaiton.

    Inspiring
    June 7, 2022

    did you try the Function() alternative as

     

    dog1 = "Poodle";
    dog2 = "Greyhound";
    dog3 = "Beagle";
    dog4 = "Border Terrier";
    dog5 = "Blood Hound";
    dog6 = "West Highland Terrier";
    dog7 = "Alsatian";
    dog8 = "Scooby Doo";
    let dogs = [];
    for (let i=1; i<9; i++){
        let value = Function('return dog'+i)()
        dogs.push(value)
    }
    console.log(dogs)
    
    osgood_Author
    Braniac
    June 9, 2022

    OK, that is a new bit of information.

    You said JSON.

    If data above is what is there it is NOT JSON.
    If you have a JSON file or string your reading from it would be valid and that is a whole different thing. You have specific Javascript you would parse the JSON and render, this also is way less code then the above solutions as well.


    quote

    OK, that is a new bit of information.

    You said JSON.

    If data above is what is there it is NOT JSON.


    By @Liam Dilley

     

    Not sure what it is then, the API claims its json, looks like json to me. I get the data back the same way as I've always retrieved json data from an API.

     

    Seems I was just over complicating the issue. The key was to use the bracket notation instead of the dot notation, nothing more required.