简体   繁体   English

如何获取随机json数据并附加到div元素

[英]how to get random json data and append to div element

Say this is my json 说这是我的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 同样继续但有3种类型1员工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 现在我的问题是我想随机获取这些json数据并附加到我使用以下代码的div元素

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. Plz帮助我如何从json文件中随机获取json数据并附加到div。

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 . 在这里,我循环遍历唯一的数组,而不是data并使用数组的值作为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 一个简单的演示

You should create a 'Deck' and fill it with the json data. 你应该创建一个'Deck'并用json数据填充它。

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: 将此代码添加到js文件的顶部:

jQuery.removeFromArray = function(value, arr) {
    return jQuery.grep(arr, function(elem, index) {
        return elem !== value;
    });
};

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM