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

Community Tip - New to the community? Learn how to post a question and get help from PTC and industry experts! X

For Each loop in ThingWorx

ranjank
15-Moonstone

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,

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
});

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

Announcements


Top Tags