【问题标题】:Split string into two variables - getting undefined is not a function将字符串拆分为两个变量 - 未定义不是函数
【发布时间】:2015-01-06 17:56:45
【问题描述】:

我正在使用一个 for 循环,里面有一个 foreach 循环。在 foreach 循环中,我得到我需要的信息并将其回显到这样的字符串:19:00^-2,2° C。我想将时间(19:00)放在一个变量中,将温度(-2,2° C)放在另一个变量中,这样我就可以将它们用于the chart。但是我不知道如何将这两个拼接起来。这是目前的样子:

var yr = localStorage.yr.split('|');
time = yr[16].split('~');
temp = time.split('^');

lineChartData = {
    labels: time,

    datasets: [{
        label: "My Second dataset",
        fillColor: "rgba(151,187,205,0.2)",
        strokeColor: "rgba(151,187,205,1)",
        pointColor: "rgba(151,187,205,1)",
        pointStrokeColor: "#fff",
        pointHighlightFill: "#fff",
        pointHighlightStroke: "rgba(151,187,205,1)",
        data: temp
    }]
}

使用此代码,我在temp = time.split('^'); 上得到Uncaught TypeError: undefined is not a functionyr[16] 打印以下内容:

19:00^-2,2° C~20:00^-2° C~21:00^-2° C~22:00^-1,9° C~23:00^-1,6° C~00:00^-1,5° C~01:00^-1,3° C~02:00^-1,1° C~03:00^-0,9° C~04:00^-0,9° C~05:00^-1,1° C~06:00^-1,3° C~07:00^-1,6° C~

PHP 代码如下所示:

for($i = 0; $i < 13; $i++) {
    $datetime           = date('Y-m-d\TH', strtotime('+'.$i.' hour'));
    $forecast_period    = $forecast->xpath('(//product/time[contains(@datatype, "forecast")][contains(@from, "'.$datetime.':00:00Z")][contains(@to, "'.$datetime.':00:00Z")])');

    foreach($forecast_period AS $period) {
        $period_datetime                    = date('H:i', strtotime($period->attributes()->from));
        $period_temperature             = $period->location->temperature->attributes()->value;
        $period_temperature_dewpoint        = $period->location->dewpointTemperature->attributes()->value;
        $period_temperature_unit            = $period->location->temperature->attributes()->unit;
        $period_wind_direction              = $period->location->windDirection->attributes()->name;
        $period_wind_direction_degrees      = $period->location->windDirection->attributes()->deg;
        $period_wind_speed                  = $period->location->windSpeed->attributes()->mps;
        $period_fog                     = $period->location->fog->attributes()->percent;
        $period_cloudiness                  = $period->location->cloudiness->attributes()->percent;
        $period_cloudiness_low              = $period->location->lowClouds->attributes()->percent;
        $period_cloudiness_medium           = $period->location->mediumClouds->attributes()->percent;
        $period_cloudiness_high         = $period->location->highClouds->attributes()->percent;
        $period_pressure                    = $period->location->pressure->attributes()->value;
        $period_pressure_unit               = $period->location->pressure->attributes()->unit;
        $period_humidity                    = $period->location->humidity->attributes()->value;

        echo $period_datetime.'^'.temp($period_temperature, $period_temperature_unit, 'not-normal').'~';
    }
}

我通过simplexml_load_file()YR's API 获取信息($forecast 是使用simplexml_load_file() 从 API 获取 XML 数据的人)。

话虽如此,我的问题是:如何将yr[16] 中的时间和温度放入两个变量中而不会出现任何错误,从而构建图表?

【问题讨论】:

  • 你知道你可以简单地使用 JSON 吗?
  • time 是一个数组。 split() 必须用于字符串,而不是数组。
  • 您对temp 的期望是什么? time有13个时间和温度。
  • @Barmar 哦。我没想到。我将temp = time.spl... 更改为temp = yr[16].spl... 并输出:["20:00", "-2° C~21:00", "-2° C~22:00", "-1,9° C~23:00", "-1,6° C~00:00", "-1,5° C~01:00", "-1,3° C~02:00", "-1,1° C~03:00", "-0,9° C~04:00", "-0,9° C~05:00", "-1,1° C~06:00", "-1,3° C~07:00", "-1,6° C~08:00", "-1,8° C~"]。现在我必须拆分数组的其余部分,但不是第一个。如何? :)
  • @Barmar 是的。我知道。我想提取未来 13 小时的时间和温度并将其用于图表。

标签: javascript php arrays split


【解决方案1】:

你可以使用Array.prototype.map:

var time_and_temp = time.map(function(el) {
    var temp = el.split('^');
    return { time: temp[0], temperature: temp[1] };
});

这会将time_and_temp 设置为如下数组:

[ { time: "19:00",
    temperature: "-2,2° C"
  },
  { time: "20:00",
    temperature: "-2° C"
  }
  ...
]

【讨论】:

  • 谢谢!但是如何将输出(我想第一行用time_and_temp[0] 提取)插入到图表中?我应该在图表周围放置一个for 循环吗?
  • 我不知道你的图表期望什么。您可以使用time_and_temp[0].time 获取时间,使用time_and_temp[0].temperature 获取温度。
  • 您可以通过单击我的问题中的链接找到有关图表如何工作的所有信息。但长话短说:labels 打印时间,data 打印温度。您的变量只打印一次时间和温度。因此我必须使用for 循环,但我不知道我应该把它放在哪里。围绕图表 (for(...) { lineChartData = { ... } })?
【解决方案2】:

yr16 = yr[16].toString();

time = yr16.split('^')[0];

temp = yr16.split('^')[1];

您需要在.split('^') 之后包含[0][1],以指示将哪个部分(^ 之前或之后)分配给变量。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2020-12-27
    • 2020-04-11
    • 1970-01-01
    • 1970-01-01
    • 2015-11-30
    • 2013-12-28
    • 1970-01-01
    • 2020-09-12
    相关资源
    最近更新 更多