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

Community Tip - Did you get called away in the middle of writing a post? Don't worry you can find your unfinished post later in the Drafts section of your profile page. X

For Each loop in ThingWorx

ranjank
14-Alexandrite

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 6

Hello @ranjank ,

 

Can you try using 

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

 

Hope it helps,

ranjank
14-Alexandrite
(To:Ciprian-Traian)

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. 

 

lbisax
13-Aquamarine
(To:ranjank)

for your example the forEach synthax would be:

 

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

odukiya
15-Moonstone
(To:ranjank)

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

Hi, 

 

What should be the output basetype to get the result of this for loop.

 

Help me in (abineshabd17@gmail.com)

 

Thanks,

Abinesh Loganathan

slangley
23-Emerald II
(To:ranjank)

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

Top Tags