Picking A Random Json Object
I've got json data. [ ['Mango','M'], ['Lychee','L'], ['Pineapple','P'], ['Banana','B'] ] I need to be able to pick an Array item randomly (e.g. ['Pineapple','P']). How can I do a
Solution 1:
Just take Math.random()
and the length of the array as factor.
var array = [
["Mango","M"],
["Lychee","L"],
["Pineapple","P"],
["Banana","B"]
];
var randomItem = array[Math.random() * array.length | 0];
// take only the element with index 0alert(randomItem[0]);
Post a Comment for "Picking A Random Json Object"