Page 1 of 1

JavaScript working with an array

Posted: Wed Mar 13, 2019 4:42 am
by cwswitch
Hi,

I get this info

Code: Select all

info = (job.getVariableAsString('[Job.NameProper:After="(",Before=")"]'));
It looks like this 10,5,10,20,10,10,20,10

I'm looking to put that info into an array and then access parts of the array

So far any effort to put it into an array or access parts of that result in 'Undefined'.

Can anyone please let me know how to put the info into an array and then access parts of that array?

(or another way if this is the wrong way)

EDIT I forgot to mention that I'm aiming to access the part of the array with another variable. So when I want to access part x of the array, then I'll be using a variable which equates to x.

Thanks!

Re: JavaScript working with an array

Posted: Wed Mar 13, 2019 7:30 am
by r.zegwaard
Hi

I guess this should do the trick ;)

Code: Select all

infoString = job.getVariableAsString('[Job.NameProper:After="(",Before=")"]');
infoArray = infoString.split(",");

Re: JavaScript working with an array

Posted: Wed Mar 13, 2019 10:22 pm
by cwswitch
Thanks for the help!

Is it possible to echo variables from a script out to somewhere to see what a script is producing?

I think I was making the array OK, but trying to echo out an array (so undefined) and not echo out a position of the array. Silly me :roll:

Re: JavaScript working with an array

Posted: Thu Mar 14, 2019 1:01 am
by gabrielp
cwswitch wrote: Wed Mar 13, 2019 10:22 pm Is it possible to echo variables from a script out to somewhere to see what a script is producing?
You could log a variable and read it there:

Code: Select all

var myCoolVariable = [1,2,3,4,5]
job.log(1, "MyCoolVariable: " + myCoolVariable.toString() );
var myCoolIndex = 3
job.log(1, "Selected value for index " + myCoolIndex.toString() + ": " + myCoolVariable[myCoolIndex].toString() );
Which would probably log something like the following:
MyCoolVariable: 1,2,3,4,5
Selected value for index 3: 4

Re: JavaScript working with an array

Posted: Thu Mar 14, 2019 3:21 am
by cwswitch
THIS makes my life much easier. - Thanks

Re: JavaScript working with an array

Posted: Thu Apr 11, 2019 8:31 am
by rashirane579
Excllent topic! Thanks for such an helpful thread. It was a helpful post for me as I had a same question.

Re: JavaScript working with an array

Posted: Wed Apr 17, 2019 5:01 pm
by kwoodVisions
Wouldn't the index of 3 then be 2 and not 3 since indexing starts at 0, not 1.