LuisMa Suárez Gutiérrez
Posts: 0
Joined: Mon Aug 18, 2014 9:12 pm

how to visually map a JSON array from DB return value[ math.random() ]

hi !
i did some research and looked at this link :
Mapping from Array column in DB

But i didnt get the answer i was looking for

I want my images's asset URL to look like be randomized from
fotos= [
"//kwarcens.sirv.com/ElevationBurger%2001.png" ,
//kwarcens.sirv.com/ChillAkil%2001.jpg" ,
"//kwarcens.sirv.com/500Noches%2001.jpg"
]
period.
But it doesnt work , keep getting the element[0]

this is the code i put in the js windows in the service mapping screen

var x= Math.floor((Math.random() * 2) + 1);
return value.(x)

this is my service response

Image

and this my mapping:
Image

and this is my JS :
Image

looks rock solid to me..
can you help me find out what im missing?

simply put, I want to add several picture for an entry and randomize on each service call.

Serhii Kulibaba
Posts: 150
Joined: Tue Aug 27, 2013 1:47 pm

how to visually map a JSON array from DB return value[ math.random() ]

Hello,

Do you need to get only one random photo? If so please use JS below on that mapping:
prereturn value[Math.floor(Math.random()*value.length)];/pre

LuisMa Suárez Gutiérrez
Posts: 0
Joined: Mon Aug 18, 2014 9:12 pm

how to visually map a JSON array from DB return value[ math.random() ]

hi
it didnt work. value isin't an array to begun with .
Notice the mapping.
it's already maps fotos ( a string), not the entire array

Image

it seems im getting a nullpointer.
i tried
return value[Math.floor(Math.random()*value.length)];
and also tried
return value[0];

neither worked

Serhii Kulibaba
Posts: 150
Joined: Tue Aug 27, 2013 1:47 pm

how to visually map a JSON array from DB return value[ math.random() ]

Please remove/add mapping arrows as it is shown on the screenshot below:
Image

And add JS to the new arrow:
prevar randPhoto = value.fotos;
element.find("name=img_main").attr("src", randPhoto[Math.floor(Math.random()*randPhoto.length)];/pre

LuisMa Suárez Gutiérrez
Posts: 0
Joined: Mon Aug 18, 2014 9:12 pm

how to visually map a JSON array from DB return value[ math.random() ]

Sorry Sergiy,
It didnt work.
It says "name=img_main" doesnt exist ot isin't a valid identifier.
Seems like a standard Jquery issue,
I'm probably going to need this eventually. But for now I dropped the whole random picture idea,

Return to “Issues”