Skip to main content
1-Visitor
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

5-Regular Member
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,

Translate
ranjank1-VisitorAuthor
1-Visitor
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. 

 

Translate
13-Aquamarine
Jan 5, 2021 12:49 PM UTC

for your example the forEach synthax would be:

 

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

Translate
Support
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

Translate