Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

jQuery pick a random value from a array of strings

I know how do this in PHP, but in javascript arrays are weird.

So I have a set of image transitions which have effects that use easing equations. For certain effects I want to pick up a random value from a array of multiple values:

something like:

easing: randomFrom(array('easeOutElastic', 'easeOutBounce', 'easeOutSince')),

like image 364
Alex Avatar asked Dec 02 '22 02:12

Alex


2 Answers

function randomFrom(array) {
  return array[Math.floor(Math.random() * array.length)];
}

// in your code
easing: randomFrom(['easeOutElastic', 'easeOutBounce', 'easeOutSince']),
like image 103
Dogbert Avatar answered Dec 19 '22 05:12

Dogbert


Try this:

function randomFrom(arr){
    var randomIndex = Math.floor(Math.random() * arr.length);
    return arr[randomIndex];
}
like image 41
Naftali Avatar answered Dec 19 '22 06:12

Naftali