cancel
Showing results for 
Search instead for 
Did you mean: 
cancel
Showing results for 
Search instead for 
Did you mean: 

For Each loop in ThingWorx

Aquamarine

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.

5 REPLIES 5

Re: For Each loop in ThingWorx

Hello @ranjank ,

 

Can you try using 

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

 

Hope it helps,

Re: For Each loop in ThingWorx

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. 

 

Re: For Each loop in ThingWorx

for your example the forEach synthax would be:

 

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

Re: For Each loop in ThingWorx

Hello @ranjank 

 

The forEach() method calls a function once for each element in an array, in order.

To use foreach you should use something like.

var sum=0;

var numbers = [1234];
numbers.forEach(myFunction);

function myFunction(item) {
  sum += item;
  }

 

Thanks

Om Dukiya

Re: For Each loop in ThingWorx

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

Announcements

Thingworx Navigate content has a new home! Click here to access the new Thingworx Navigate forum!