creating an array of objects in switch scripter

Post Reply
bkromer
Member
Posts: 99
Joined: Thu Jul 11, 2019 10:41 am

creating an array of objects in switch scripter

Post by bkromer »

Hello,
I want to create an array of objects.
The result should look like this:

Code: Select all

[{'nr':'128947','version':'02','bezeichnung':'STR COLD'},{'nr':'123123','version':'05','bezeichnung':'SAM FOUR'}];
My code so far:

Code: Select all

var documentArray = [];
for (k=start; k<end; k++ ) {   
	dokumentennummernRE.search( matches[k] );
	dokumentArray[k] = {'nr':dokumentennummernRE.cap(1) 'version':dokumentennummernRE.cap(4) 'bezeichnung':dokumentennummernRE.cap(5)
	};
			}
			

Even an array of arrays would do. But I always getting parse errors.
Benjamin
bkromer
Member
Posts: 99
Joined: Thu Jul 11, 2019 10:41 am

Re: creating an array of objects in switch scripter

Post by bkromer »

Gosh I missed the commas.... :roll:
This works now:

Code: Select all

dokumentArray[k] = {'nr':dokumentennummernRE.cap(1), 'version':dokumentennummernRE.cap(4), 'bezeichnung':dokumentennummernRE.cap(5)};
But now I need to access the objects and I am getting "undefined member" errors.
I tried:

Code: Select all

dokumentArray[k].Nr
When I

Code: Select all

job.log(1,dokumentArray); 
I get [object],[object] wich is correct. But how can i access them? Do I need to parse them before?
Benjamin
Padawan
Advanced member
Posts: 358
Joined: Mon Jun 12, 2017 8:48 pm
Location: Belgium
Contact:

Re: creating an array of objects in switch scripter

Post by Padawan »

I don't think your object is actually filled in. Can you try this to check?
job.log(1,JSON.stringify(dokumentArray));

The documentation stays that search returns a number object, so is it possible your cap() functions don't return anything.
https://www.enfocus.com/manuals/Develop ... tring.html

It's always best to consult the Enfocus documentation, no general Javascript documentation.

If you can give some more details on what you are trying to achieve, then we might be able to help you.
bkromer
Member
Posts: 99
Joined: Thu Jul 11, 2019 10:41 am

Re: creating an array of objects in switch scripter

Post by bkromer »

Okay now its more clear to me whats going on.
On the first iteration dokumentArray is:

Code: Select all

[{"bezeichnung":"FALTANWEISUNG BOGEN \r\n","nr":"2111216","version":"03"},{"bezeichnung":"SICHERHEITSINFORMATIO \r\n","nr":"2519732","version":"03"}]
on the second iteration its:

Code: Select all

[null,null,{"bezeichnung":"FALTANWEISUNG BLATT \r\n","nr":"2099769","version":"02"},{"bezeichnung":"BEILAGEZETTEL iE10-S4 \r\n","nr":"2094921","version":"02"}]
In my code, I try to read an Order and post it to an API.
The order has multiple positions. Every Position has two document lines.
The Order looks like this:
Ordernumber: 102940912470

Pos 001 Material 123123
Dokument Nr 123123 Version 02 Bezeichnung Lalalala
Dokument Nr 345355 Version 05 Bezeichnung Blablabla
Pos 002 Material 456456
Dokument Nr 462363 Version 08 Bezeichnung Didadiada
Dokument Nr 023952 Version 10 Bezeichnung Wdwdwdd
This makes it really hard to access all of it correctly.

I need to loop through Positions and make an API Call there and then make an object on my loop for the documents and afterwards make an API Call on the id of the first record with the right document objects.
Benjamin
bkromer
Member
Posts: 99
Joined: Thu Jul 11, 2019 10:41 am

Re: creating an array of objects in switch scripter

Post by bkromer »

I have managed to make it work with the push method. For every iteration of the documents I push an object for every document line to the "documentArray" Array afterwards, I can use dot notation as the ordering is always the same.

Like:

Code: Select all

for (k=start; k<end; k++ ) { 
  dokumentennummernRE.search( matches[k]);
  dokumentArray.push({
  'nr':dokumentennummernRE.cap(1), 
  'version':dokumentennummernRE.cap(4), 
  'bezeichnung':dokumentennummernRE.cap(5)
  });		
Works now perfectly. Thanks for the support. The stringify method helped a lot. :)
Benjamin
Padawan
Advanced member
Posts: 358
Joined: Mon Jun 12, 2017 8:48 pm
Location: Belgium
Contact:

Re: creating an array of objects in switch scripter

Post by Padawan »

You're welcome!
Post Reply