I have a JSON object which consists of a long list of other JSON objects which have some common properties to each other such :
var myData = {
"0291" : { "Firstname" : "Jeremy", "Surname" : "Dyson" },
"0398" : { "Firstnname" : "Billy", "Surname" : "Bunter" },
"6714" : { "Firstnname" : "Harry", "Surname" : "Peterson" },
"9080" : { "Firstnname" : "Barry", "secondname": "Joe", "Surname" : "Mainwaring"}
...
...
}
I already built an html template. With the JS, I want to pick or iterate (random pick + loop) through the objects in data{} in random order, so I can fill up the HTML on the fly for each visitor. The random part is important, so each visitor likely get a different data.
Plain JavaScript or jQuery solutions will work in the context in which this is being deployed.
EDIT: Solution I implemented is below.
1. Collect all keys :
var keyArray = Object.keys(myData);
2. Shuffle function:
function shuffle(o){ //v1.0
for(var j, x, i = o.length; i; j = parseInt(Math.random() * i), x = o[--i], o[i] = o[j], o[j] = x);
return o;
};
keyArray = shuffle(keyArray); // shuffle it!
3. Loop to iterate:
for (var i = 0; i < keyArray.length; ++i) {
var current = data[keyArray[i]];
... // what you want to do each time.
}