Я использую цикл for с петлей foreach внутри. В цикле foreach я получаю необходимую информацию и повторяю ее так: 19:00^-2,2° C
Я хочу поместить время (19:00
) в одну переменную и температуру (-2,2° C
) в другую переменную, чтобы я мог использовать их в диаграмме. Но я не знаю, как я могу объединить эти два. Вот как это выглядит на данный момент:
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
}]
}
С помощью этого кода я получаю Uncaught TypeError: undefined is not a function
на temp = time.split('^');
, yr[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').'~';
}
}
Я получаю информацию от YR API через simplexml_load_file()
($forecast
- это тот, кто получает XML-данные из API с помощью simplexml_load_file()
).
С учетом сказанного здесь мой вопрос: как я могу поместить время и температуру из yr[16]
в две переменные без каких-либо ошибок и, следовательно, построить диаграмму?
Вы можете использовать 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
для получения температуры.
yr16 = yr[16].toString();
time = yr16.split('^')[0];
temp = yr16.split('^')[1];
Вы должны включить [0]
и [1]
после .split('^')
чтобы указать, какая часть (до или после ^
) назначается переменной.
time
это массив.split()
должен использоваться для строки, а не для массива.