简体   繁体   中英

Pass array to Flot chart

As we know normally we can pass array as following to Flot Aria Chart.

var data2 = [
        [0, 1], [1, 0], [2, 2], [3, 0], [4, 1], [5, 3], [6, 1], [7, 5], [8, 2], [9, 3], [10, 2], [11, 1], [12, 0], [13, 2], [14, 8], [15, 0], [16, 0]
    ];

I'm getting an json output from a file and i need to convert that as json object like 'data2' in upper code. i'm running a loop like this.

var data1 = [];
    var json = $.getJSON( "config/tsconfig.json", function () {
        $.each( json.responseJSON, function( i, item ) {

        });
    });

And inside of that json file like this.

[[2,"2017-09-27",50,30,25],[2,"2017-09-28",70,50,49],[3,"2017-09-27",50,45,5],[3,"2017-09-28",100,95,90]]

How can i make a json object as in code one. any suggestions to put inside $.each loop?

You have to use map method.

Also, $.getJSON is executing asynchronous , so you need to use a callback function.

getResponse(callback){
    $.getJSON( "config/tsconfig.json", function (response) {
      let result=response.map(function(item){
         return [item[0],item[2]];
       });
       callback(result);
    });
}

getResponse(function(response){
  console.log(response);
});

 let array=[[2,"2017-09-27",50,30,25],[2,"2017-09-28",70,50,49],[3,"2017-09-27",50,45,5],[3,"2017-09-28",100,95,90]] array=array.map(function(item){ return [item[0],item[2]]; }); console.log(array); 

Using arrow functions .

array=array.map(item=> [item[0],item[2]]);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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