Skip to main content
1-Visitor
April 16, 2020
Solved

Using forEach to iterate through data being pulled from JSON in infotable

  • April 16, 2020
  • 5 replies
  • 5916 views

I am getting an issue saying 'for each is only available in Mozilla JavaScript extensions (use moz option)'. I am trying to add rows to my infotable using data being pulled in from a JSON file. 

 

Is there another way to do this? Thanks!

Best answer by morganweiss33

data.array[i].isbn ended up working for me, thank you!

5 replies

5-Regular Member
April 17, 2020

for each should work in Twx, even if not, you can change to use for (i<row) command to iterate the data

15-Moonstone
April 17, 2020

Yes, Ryan is correct, ThingWorx uses Rhino JS Engine thus supports for each

Community Manager
April 17, 2020

Hi @morganweiss33.

 

Try changing this row:  for (var i=0; i < data.rows;i++) {

to:  for (var i=0; i < data.array.length;i++) {

 

Refer to this article for more information.

 

Regards.

 

--Sharon

 

Community Manager
April 27, 2020

Hi @morganweiss33.

 

If one of the previous responses answered your question, please mark the appropriate one as the Accepted Solution for the benefit of others with the same question.

 

Regards.

 

--Sharon

18-Opal
April 27, 2020

Hello @morganweiss33,

 

tl;dr Use for(var i = 0..... as @slangley suggests.

 

ThingWorx infotables look like JavaScript arrays, but they are not. You can use forEach and its friends, but to do it you have to wrap infotable rows into js Array explicitly like that:

var nativeArray = Array.from(myInfotable);

 

Here's a very useful (!) example of this technique, which randomly disables half of your GenericThings:

var result = 'Disabled ' + Array.from(ThingTemplates["GenericThing"].QueryImplementingThings({ maxItems: 100000 }))
	.map(t => Things[t.name])
	.filter(t => {
		if (Math.random() < 0.5 && t.IsEnabled()) {
			t.DisableThing();
			return true;
		} else {
			return false;
		}
	}).length + ' things';

 

That looks cool, but apparently it requires some data copying (sadly Rhino !== V8) and thus works slower than for-based approach straight from ALGOL. I don't remember the right figures, but for me it was something like twice slower, on that scale.

 

Regards,
Constantine

morganweiss331-VisitorAuthorAnswer
1-Visitor
April 28, 2020

data.array[i].isbn ended up working for me, thank you!