簡體   English   中英

如何將幾個 arrays 合二為一

[英]how to join several arrays in one

我有這個數組

[{"temp":"24.44","time":"2021-11-26 22:23:29.370657"},
 {"temp":"25.44","time":"2021-11-26 22:23:35.411530"}]

我需要這個

data.addRows([
    [24.44, [22, 23, 29]], 
    [25.44, [22, 23, 35]]
  ]);

就是能做個圖表,謝謝你的幫助。

您可以使用map並拆分以達到所需的結果。

 const arr = [ { temp: "24.44", time: "2021-11-26 22:23:29.370657" }, { temp: "25.44", time: "2021-11-26 22:23:35.411530" }, ]; const result = arr.map((o) => [ +o.temp, o.time.split(" ")[1].split(".")[0].split(":").map(Number), ]); console.log(result);

  google.charts.load('current', {'packages':['line', 'corechart']});
  google.charts.setOnLoadCallback(drawChart);

function drawChart() {

  
  var chartDiv = document.getElementById('chart_div');

  var data = new google.visualization.DataTable();
  data.addColumn('timeofday', 'Fecha');
  data.addColumn('number', "Average Temperature");
  
  

  var materialOptions = {
    chart: {
      title: 'Average Temperatures and Daylight in Iceland Throughout the Year'
    },
    width: 900,
    height: 500,
    series: {
      // Gives each series an axis name that matches the Y-axis below.
      
     
    },
    axes: {
      // Adds labels to each axis; they don't have to match the axis names.
      y: {
        Temps: {label: 'Temps (Celsius)'},
        
      }
    }
  };

  

  var materialChart = new google.charts.Line(chartDiv);
   

   setInterval(function() {
                var JSON = $.ajax({url:"http://localhost/maintpro/datos_sensor.php?q=1",dataType: 'json',async: false}).responseText;
                const respuesta = jQuery.parseJSON(JSON);

                const result = respuesta.map((o) => [
                    o.time.split(" ")[1].split(".")[0].split(":").map(Number),
                    +o.temp,
                ]);

            
                data.addRows(result);
                materialChart.draw(data, materialOptions);
            }, 1300);

}

在此處輸入圖像描述

它正在工作,但會發生這種情況,請幫助我

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM