将数组传递给 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]]);