JavaScript working with an array

Post Reply
cwswitch
Member
Posts: 71
Joined: Fri Feb 24, 2017 12:25 am

JavaScript working with an array

Post by cwswitch » Wed Mar 13, 2019 4:42 am

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!

r.zegwaard
Member
Posts: 69
Joined: Fri Jul 08, 2011 10:31 am
Location: The Netherlands

Re: JavaScript working with an array

Post by r.zegwaard » Wed Mar 13, 2019 7:30 am

Hi

I guess this should do the trick ;)

Code: Select all

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

cwswitch
Member
Posts: 71
Joined: Fri Feb 24, 2017 12:25 am

Re: JavaScript working with an array

Post by cwswitch » Wed Mar 13, 2019 10:22 pm

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:

User avatar
gabrielp
Advanced member
Posts: 636
Joined: Fri Aug 08, 2014 4:31 pm
Location: Boston
Contact:

Re: JavaScript working with an array

Post by gabrielp » Thu Mar 14, 2019 1:01 am

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
Free Switch scripts: open-automation @ GitHub
Free Switch apps: open-automation @ Enfocus appstore

If I've helped you and you'd like to say thanks, consider adding me on LinkedIn and leaving me an endorsement or recommendation. It helps a lot!

cwswitch
Member
Posts: 71
Joined: Fri Feb 24, 2017 12:25 am

Re: JavaScript working with an array

Post by cwswitch » Thu Mar 14, 2019 3:21 am

THIS makes my life much easier. - Thanks

rashirane579
Newbie
Posts: 1
Joined: Thu Apr 11, 2019 8:29 am

Re: JavaScript working with an array

Post by rashirane579 » Thu Apr 11, 2019 8:31 am

Excllent topic! Thanks for such an helpful thread. It was a helpful post for me as I had a same question.

kwoodVisions
Newbie
Posts: 9
Joined: Mon Oct 08, 2018 9:43 pm

Re: JavaScript working with an array

Post by kwoodVisions » Wed Apr 17, 2019 5:01 pm

Wouldn't the index of 3 then be 2 and not 3 since indexing starts at 0, not 1.

Post Reply