Need help creating loop

How would I create a loop for this,

$w("#dropTier").options = [
  {"label": "" + tierss[0].TierName, "value":"0"},
  {"label": "" + tierss[1].TierName, "value":"1"},
  {"label": "" + tierss[2].TierName, "value":"2"},
  {"label": "" + tierss[3].TierName, "value":"3"},
  {"label": "" + tierss[4].TierName, "value":"4"},
  {"label": "" + tierss[5].TierName, "value":"5"},
];

Instead of writing it all out, I would need to do it many times on my page and I know there is an easier way. I have been trying to find a way to do it for a few hours and my brain hurts.

Thanks

You could add the options to a js file once and then call them whenever you need to populate a dropdown.

//filename.js file

let tierss = [value1, value 2,...]

export const name = [
  {"label": "" + tierss[0], "value":"0"},
  {"label": "" + tierss[1], "value":"1"},
  {"label": "" + tierss[2], "value":"2"},
  {"label": "" + tierss[3], "value":"3"},
  {"label": "" + tierss[4], "value":"4"},
  {"label": "" + tierss[5], "value":"5"},
];

Then import it and call it for any dropdown that you need values for:


import {name} from 'public/filename.js'; 

$w("#dropTier").options = name;
 

Thanks.
I should have stated this before, I am getting tierss from a backend fetch call so I don’t think your solution will work. I am looking to just use a loop on the page to do it untill all the results are entered in the dropdown, the number of results changes all the time.

@shawn-johnson
Okay. Not sure how to push the values directly to the dropdown. The only way I can think of is to create an array and push the values from tierss to it with a loop. Then use the newArray options for the dropdown. Hope this helps. If not, then hope someone else can provide a better answer. Good luck.


 $w.onReady(function () { 
 
 let newArray = [];
 
 for (var i = 0; i < tierrs.length; i++) {
   newArray.push(
   {"label": "" + tierrs[l], "value": l.toString()}
   );
 }
 $w("#dropTier").options = newArray; 
})
 
 

@marcia Thanks I will try it out later and see if it works.

@shawn-johnson Marcia’s answer will work…probably. I think your explanation is a little vague and it’s hard to tell if you are using placeholders and where. Also, I think you can have numerical values, but only string labels for dropdowns.

A couple of minor fixes to Marcia’s code:

let newArray = [];
 
 for (let i = 0; i < tierrs.length; i++) {
   newArray.push({"label": tierrs[i], "value": i});
   if (i === tierrs.length - 1) $w("#dropTier").options = newArray;
 }

@skmedia Thanks David this worked perfect

@skmedia How would I do the same thing, but enter this into a var called teams

           teams.push(tiers[$w('#dropTier').value].Teams[0].TeamName),
            teams.push(tiers[$w('#dropTier').value].Teams[1].TeamName),
            teams.push(tiers[$w('#dropTier').value].Teams[2].TeamName),
            teams.push(tiers[$w('#dropTier').value].Teams[3].TeamName),
            teams.push(tiers[$w('#dropTier').value].Teams[4].TeamName),
            teams.push(tiers[$w('#dropTier').value].Teams[5].TeamName),
            teams.push(tiers[$w('#dropTier').value].Teams[6].TeamName),
            teams.push(tiers[$w('#dropTier').value].Teams[7].TeamName),
            teams.push(tiers[$w('#dropTier').value].Teams[8].TeamName),
            teams.push(tiers[$w('#dropTier').value].Teams[9].TeamName)

I’ve tried a couple thing with the last code and I can’t get it to work

@shawn-johnson What kind of variable do you need? An array? A string? Do you have a fixed amount of items in your Teams array or does it vary?

@skmedia Its an array and it varies depending on the tier that is chosen

@shawn-johnson Then try something like this:

tiers[$w('#dropTier').value].Teams.forEach(f => {
    teams.push(f.TeamName);
});

@skmedia I think I owe you a beer or ten, thanks for the help.
I still need to test a little more but my first try it work perfect.

@shawn-johnson I’ll take it!