Jquery - Select random JSON Object

后端 未结 2 1469
别跟我提以往
别跟我提以往 2021-01-25 11:27

I have this jquery code to output the entries in a JSON file on page load...

$.getJSON(\'b.json\', function(data) {
      $(\'#dictionary\').empty().hide();



          


        
相关标签:
2条回答
  • 2021-01-25 11:57

    Looks like you want a random entry from an array.

    try:

    var random_entry = entry[Math.floor(Math.random() * entry.length)]
    
    0 讨论(0)
  • 2021-01-25 12:08
    $.getJSON('b.json', function(data) { 
      var entry = data[Math.floor(Math.random()*data.length)];
      //do the same exact thing with entry
    }
    
    0 讨论(0)
提交回复
热议问题