Setting value of a temporary state in a loop

In my app, I have created a temporary state. Let's call is state1.

In my first JS query, I run a loop and set this state's value to the first value from an array.
once this value is set, I trigger another query which is a resource query.

It hits a certain API which has the following structure.

https://api.whatever/this/that/{{value of state1 as a string}}/something

Now, in the loop, it is not updating the value of the state.
The loop is supposed to run till the length of the array I mentioned.
IN every iteration, it is supposed to update the value of state and run the resource query with the value of that state updated.

But instead the loop runs to the length of the array but the state isn't updated in every iteration. It takes the last value from the array even though I am assigning it based on the index of the loop.

Suggestions or help welcomed.

@dh00mk3tu Welcome to the community!

Please share your code containing the loop.

Hey Scott, this is what I am doing.

  1. Channel ID is something that is going to part of the API endpoint but not as a query parameter. For instance ""
  2. To achieve this, I created a query which sets the value of a state to a channel id as string.
  3. The channel ID are in an array, I am iterating over this array and updating the value of the state on each iteration of the loop.

Now, the state is not being updated as it should be. If I console.log just the elements of the array,

Let's say arr=["a","b","c","d"]
The output would be the individual elements i.e a,b,c,d

for(var i =0; i<=arr.length;i++){

In the above snippet if I do this,

for(var i =0; i<=arr.length;i++){

The value of state1 is set to the last element of the array and that value is printed n number of times (length of the array basically)

Here is the actual code :

var channelIDs = channelID.value
channelIDs.replaceAll('\n', '');
channelIDs.replaceAll(' ', '');

var channelid_list = channelIDs.split(',');
console.log("hehehe "+state28.value);
for(var i = 0 ; i < channelid_list.length; i++) {

I would not use the state1 temp value:

var testArray = ["a","b","c","d"];
for(i=0; i < testArray.length; i++){ // removed the <= this was the reason for seeing only last value
 query97.trigger({ additionalScope: { variableName: testArray[i] } });
 query94.trigger({ additionalScope: { variableName: testArray[i] } });
console.log(testArray[i] + ' ' + i);

You then add {{variableName}} to each of the queries...

Understood, let me give it a try

Worked! Thanks Scott