3

Say this is my json

[
{
    "imageSmall": ["images/employee_jpgs/employees_abhishek_80x80.jpg"],
    "imageBig": ["images/employee_jpgs/employees_abhishek_150x150.jpg"],
    "name": ["Abhishek Shet"],
    "quotes": ["Just perfect to start your career after school. Makes me feel        others in the industry are way slower then us. Awesome team and and a brilliant product to work on!!!!. And most importantly I enjoy what I do :)."],
    "type": "employee"
},
{
    "imageSmall": ["images/employee_jpgs/employees_barbra_80x80.jpg"],
    "imageBig": ["images/employee_jpgs/employees_barbra_150x150.jpg"],
    "name": ["Barbra Gago"],
    "quotes": ["The best part about working at tibbr is how dynamic the environment is, there's a lot of flexibility and freedom to execute on new ideas. Because everyone is so talented, there is a ton of trust and support coming from managers and team members-we all count on each other to do the best possible job!"],
    "type": "employee"
},

the same continues but there are 3 types 1-employee 2-twitter 3-social

Now my problem is I want get these json data randomly and append to my div element I used following code

function(args){
 var me=this;
 $.getJSON(args.json,function(data) { 
 me.set(args);
 $.each(data, function(i){
    var id="randomizr_item_" + i;
    var temp= $('<div id='+ id +' class="randomizr-grid-items"><img src="'+ this.imageSmall[0]  +'" /></div>');
    me.config.container.append(temp);
    this.target=$(temp);
});

I know how to generate single random entry using following code

entry = data[Math.floor(Math.random()*data.length)];

which generates single random entry.

Plz help me how to get json data randomly from above json file and append to div.

4

2 回答 2

1

You need to make an array of random unique numbers like following:

function generateRandom(min, max) {
    var arr = [];
    while(arr.length < 5){
      var randNum = Math.floor(Math.random() * (max - min + 1)) + min,
          found=false;
      for(var i=0;i < arr.length; i++){
        if(arr[i] == randNum){found=true;break}
      }
      if(!found)arr[arr.length] = randNum;
    }
    return arr;
}

Then you need to loop over data like following:

Here I am looping over unique array, not on data and using value of array as index to data.

var orders = generateRandom(0, data.length-1);

$.each(orders, function(index, value){
    var id="randomizr_item_" + i;
    var temp= $('<div id='+ id +' class="randomizr-grid-items"><img src="'+ data[value].imageSmall[0]  +'" /></div>');
    me.config.container.append(temp);
    this.target=$(temp);
});

A simple demo

于 2012-06-03T15:57:30.223 回答
0

You should create a 'Deck' and fill it with the json data.

Once the Deck is filled, loop through it while it has elements, like this:

while(deck.length > 0) {
 var random_index = Math.floor(Math.random()*data.length);
 var item = deck[random_index];
 // do stuff with item
 deck = jQuery.removeFromArray(random_index, deck);
}

Add this code to the top of your js file:

jQuery.removeFromArray = function(value, arr) {
    return jQuery.grep(arr, function(elem, index) {
        return elem !== value;
    });
};
于 2012-06-03T14:16:44.510 回答