简体   繁体   English

将数组传递给Flot图表

[英]Pass array to Flot chart

As we know normally we can pass array as following to Flot Aria Chart. 众所周知,通常我们可以将数组如下传递给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. 我从文件中获取json输出,我需要将其转换为json对象,例如上层代码中的“ data2”。 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. 在这样的json文件中。

[[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. 我如何像代码一那样制作一个json对象。 any suggestions to put inside $.each loop? 任何建议放入$ .each循环内?

You have to use map method. 您必须使用map方法。

Also, $.getJSON is executing asynchronous , so you need to use a callback function. 另外, $.getJSON正在执行asynchronous ,因此您需要使用callback函数。

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 . 使用arrow 功能

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

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

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