Michael Pierce5821971
Posts: 0
Joined: Fri May 03, 2013 11:29 pm

Response to an array

Is there a way to just store all user id's that are returned in a response to an array so that I can randomly choose a user id on a button click event? Right now I have a service that works and returns all users, but I only need the user ids into an array so I can perform a function to just retrieve one random user id out of the array and then query back to retrieve the username.

Sergey Kozyr
Posts: 0
Joined: Tue Apr 30, 2013 2:55 pm

Response to an array

This could be done with JavaScript. You should add "Run JavaScript" action to "Success" event of your datasource on "Data" tab. Next you should write javascript code where "data" variable holds all response content. For example if you need some random element of an array you can write next code:
code
var randomIndex = Math.floor(Math.random() * data.length); //Generate random array index;
var randomUserId = data[randomIndex].id; //Get user ID from response array
localStorage.userId = randomUserId; //Store user ID in localStorage variable
/code

Michael Pierce5821971
Posts: 0
Joined: Fri May 03, 2013 11:29 pm

Response to an array

Thanks!! But the big issue was trying to put the response into a local array variable.

bahar.wadia
Posts: 0
Joined: Wed Aug 07, 2013 2:05 am

Response to an array

Yes, I have the same question. How do you create a local array and then access it. I'd like to do some math on the records received. It is not clear at all.

bahar.wadia
Posts: 0
Joined: Wed Aug 07, 2013 2:05 am

Response to an array

Yes, I have the same question. How do you create a local array and then access it. I'd like to do some math on the records received. It is not clear at all.

Anton Artyukh5836028
Posts: 0
Joined: Wed May 08, 2013 12:57 pm

Response to an array

Hello,

In Success event for service you can save "data" variable to "window.localSavedArrayFromResponse":

precodewindow.localSavedArrayFromResponse = data;/code/pre

So in any other JS-code on current screen you can access variable "window.localSavedArrayFromResponse". This variable is saved in global JS-scope for screen.

Please note, if you want to work with this data somewhere on other screen is better idea to use for saving data localStorage-object. So your code will be looks something like this:

precodelocalStorage.setItem( "localSavedArrayFromResponse", JSON.stringify( data ) );/code/pre

And on other screen you can access data as precodevar data = JSON.parse( localStorage.getItem( "localSavedArrayFromResponse" ) );/code/pre

In both cases you can rename variable.

Return to “Issues”