Skip to main content
Jan 5, 2021 9:21 AM UTC
Question

For Each loop in ThingWorx

Hi Team,

 

Could somebody help me with the snippet alongwith example that how to write ForEach loop of JS in ThingWorx.

 

Suppose my array is var fruits = ["apple", "orange", "cherry"]; , then how to print each element (or access each element) of this array.

 

Thanks in advance.

6 replies

Jan 5, 2021 11:59 AM UTC

Hello @ranjank ,

 

Can you try using 

for (i = 0; i < fruits.length; i++) {
result = fruits[i];
}

 

Hope it helps,

ranjankAuthor
Jan 5, 2021 12:03 PM UTC

Hi @Ciprian-Traian

 

It is a normal For Loop and I am looking for For Each loop.

 

The array which I have mentioned is just an example.

 

Please advise. 

 

Jan 5, 2021 12:49 PM UTC

for your example the forEach synthax would be:

 

fruits.forEach (element => {
      // execute stuff for element
});

Jan 12, 2021 8:55 PM UTC

Hi @ranjank.

 

If you feel your question has been answered, please mark the appropriate response as the Accepted Solution for the benefit of others with the same question.

 

Regards.

 

--Sharon