繁体   English   中英

将数组传递给Flot图表

[英]Pass array to Flot 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]
    ];

我从文件中获取json输出,我需要将其转换为json对象,例如上层代码中的“ data2”。 我正在运行这样的循环。

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

        });
    });

在这样的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]]

我如何像代码一那样制作一个json对象。 任何建议放入$ .each循环内?

您必须使用map方法。

另外, $.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); 

使用arrow 功能

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

暂无
暂无

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

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