Page 1 of 1

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

Posted: Fri Apr 22, 2016 7:34 am
by LuisMa Suárez Gutiérrez

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.


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

Posted: Fri Apr 22, 2016 3:28 pm
by Serhii Kulibaba

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


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

Posted: Sun Apr 24, 2016 6:41 am
by LuisMa Suárez Gutiérrez

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


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

Posted: Mon Apr 25, 2016 12:24 pm
by Serhii Kulibaba

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


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

Posted: Mon May 02, 2016 7:52 am
by LuisMa Suárez Gutiérrez

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,