how to get randomize the json data

388
November 21, 2016, at 8:19 PM

i have a table whose data is coming from a jsonfile. Let say, the json file have 10 sets of data. but in my table i want randomize the no. of data. This is my json

 {
"Payload": [
    {
        "srcFileName": "02-Blk65536Alk_CCN1-9967-20150521223150-12975",
        "srcFileSize": "1048576"
    },
    {
        "srcFileName": "04-Blk65536Blk_GGSN1-9967-20150521223150-10375",
        "srcFileSize": "1048576"
    },
    {
        "srcFileName": "01-BlkWE536Xlk_CCN1-9967-20150521223150-10575",
        "srcFileSize": "1048576"
    },
    {
        "srcFileName": "02-Blk65236Blk_SGSN1-9967-20150521223150-15775",
        "srcFileSize": "1048576"
    },
    {
        "srcFileName": "04-BlkDF5236Blk_CCN1-9967-20150521223150-15775",
        "srcFileSize": "1048576"
    },
    {
        "srcFileName": "05-Blk65236Blk_CCN1-9967-20150521223150-15775",
        "srcFileSize": "1048576"
    },
    {
        "srcFileName": "08-Blk65236Blk_CCN1-9967-20150521223150-15775",
        "srcFileSize": "1048576"
    },
    {
        "srcFileName": "02-Blk65236Blk_SASN1-9967-20150521223150-15775",
        "srcFileSize": "1048576"
    }
 ]}

Normally if I want to fetch all data of the json i wrote that like

 $.getJSON('resources/json/monitor_precessed_files.json', function(data) {
        jQuery.each(data.Payload, function (index, value) {
            $('#processed_files').append('  <li title="' + value.srcFileSize + " " + '"class="list-group-item"> <span class="glyphicon glyphicon-file"></span><small>'
                    + value.srcFileName + '</small></li>');
        });
 });

But I dont want the all data. i want the no of data sholud be randomize. please help

Answer 1

Get a random element from array using instead of using $.each

$.getJSON('resources/json/monitor_precessed_files.json', function(data) {
    var len = data.Payload.length;
    //Set no of items to fetch
    var itemsToFetch = Math.floor(Math.random()*len); 
    //Fetch random items
    for(var i=0; i <= itemsToFetch; i++){
        //Get Random element
        var payload = data.Payload[Math.floor(Math.random()*len)];
        $('#processed_files').append('  <li title="' + payload.srcFileSize 
            + " " + '"class="list-group-item"> <span class="glyphicon glyphicon-file"></span><small>'
            + payload.srcFileName 
            + '</small></li>');
    }
});
Rent Charter Buses Company
READ ALSO
Play Scala Action not receiving Multipart Form Data

Play Scala Action not receiving Multipart Form Data

I want to upload a file into my web server using Play Scala Framework. But the server is responding Bad Request (400).

396
On changing enabled property of dropdownlist in asp.net CSS/jquery doesn&#39;t work.

On changing enabled property of dropdownlist in asp.net CSS/jquery doesn't work.

i have dropdownlist in web page having enabled property false.

310
CKeditor on focus ASP.NET

CKeditor on focus ASP.NET

I use ASP. NET with CKeditor (4.

339
Converting text of textarea in a text file while uploading through php

Converting text of textarea in a text file while uploading through php

I am using a textarea for input of text in my application of PHP and using data type Varchacr2 in oracle 10g to store the text of this textarea -.

338