weixin_33694620 2017-09-28 12:06 采纳率: 0%
浏览 50

将数组传递给Flot图表

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?

  • 写回答

1条回答 默认 最新

  • weixin_33716557 2017-09-28 12:20
    关注

    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]]);
    
    </div>
    
    评论

报告相同问题?

悬赏问题

  • ¥15 关于#目标检测#的问题:大概就是类似后台自动检测某下架商品的库存,在他监测到该商品上架并且可以购买的瞬间点击立即购买下单
  • ¥15 神经网络怎么把隐含层变量融合到损失函数中?
  • ¥30 自适应 LMS 算法实现 FIR 最佳维纳滤波器matlab方案
  • ¥15 lingo18勾选global solver求解使用的算法
  • ¥15 全部备份安卓app数据包括密码,可以复制到另一手机上运行
  • ¥20 测距传感器数据手册i2c
  • ¥15 RPA正常跑,cmd输入cookies跑不出来
  • ¥15 求帮我调试一下freefem代码
  • ¥15 matlab代码解决,怎么运行
  • ¥15 R语言Rstudio突然无法启动