Community Tip - If community subscription notifications are filling up your inbox you can set up a daily digest and get all your notifications in a single email. X
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.
Hello @ranjank ,
Can you try using
for (i = 0; i < fruits.length; i++) {
result = fruits[i];
}
Hope it helps,
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.
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 = [1, 2, 3, 4];
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
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