[英]Weather forecast by pushing data from an old array into a new array using angularjs and openweathermap
大家好,我整個周末都在努力讓這件事發揮作用,但我似乎無法理解它。 我想要實現的是使用 openweathermap api 從數組中選擇天氣數據,然后將此數據推送到新數組中。 我的問題是,我想讓這項工作像 24 小時預測一樣,總是在第二天早上 06:00 開始,因此我只想推送在這個 24 小時間隔內存在的數據。 但正如我所說,我似乎無法讓它發揮作用。
這是我的代碼:
<div ng-app="weatherApp" ng-controller="weatherController">
<ul>
<li ng-repeat="x in forecastArray">
{{x.dt_txt}}
</li>
</ul>
</div>
var app = angular.module('weatherApp', []);
app.controller('weatherController', function($scope, $http) {
$scope.units = 'metric';
$scope.apiId = '...'; //My api key
$http.jsonp('http://api.openweathermap.org/data/2.5/forecast?', { params : {
q : "london",
units : $scope.units,
callback: 'JSON_CALLBACK',
APPID: $scope.apiId
}}).success(function(response){
$scope.data = response.list;
});
$scope.forecastArray = []; //new array
var dtStart = new Date(...); //Have no clue how to define this object...
var dtEnd = new Date(+new Date(dtStart) + 86400000); //dtStart + 24 hours
$scope.data.forEach(function(item){
var date = new Date(item.dt); //defining the dt property in data as a new dateobj
if(date > dtStart && date < dtEnd){
$scope.foreCastArray.push(item);
}
});
});
所以你基本上會問如何為明天早上 6 點創建一個Date
對象。
首先使用創建明天的Date
對象
var today = Date();
var tommorow = Date();
tommorow.setDate(today.getDate() + 1);
然后正確設置小時和分鍾。 使用Date.setHours(hour,min,sec,millisec)
:
tommorow.setHours(6,0,0,0);
那應該工作。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.