天气预报,使用angularjs和openweathermap将数据从旧数组推送到新数组中

Weather forecast by pushing data from an old array into a new array using angularjs and openweathermap

本文关键字:数组 新数组 angularjs 使用 openweathermap 数据 天气预报      更新时间:2023-09-26

大家好,我整个周末都在努力让这件事发挥作用,但我似乎无法理解。我试图实现的是使用openweathermapapi从数组中提取weatherdata,然后将这些数据推送到新的数组中。我的问题是,我想让这项工作像一个总是在第二天早上06:00开始的24小时预测,因此我只想推送这24小时间隔内存在的数据。但正如我所说的,我似乎无法让它发挥作用。

这是我的代码:

HTML

<div ng-app="weatherApp" ng-controller="weatherController">
    <ul>
        <li ng-repeat="x in forecastArray">
            {{x.dt_txt}}
        </li>
    </ul>
</div>

JavaScript

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);
        }
    });
});

所以您基本上会问如何为tommorow 6am创建Date对象。

首先使用为tomorow创建Date对象

var today = Date();
var tommorow = Date();
tommorow.setDate(today.getDate() + 1);

然后正确设置小时和分钟。使用Date.setHours(hour,min,sec,millisec):

tommorow.setHours(6,0,0,0);

这应该行得通。