天气预报,使用angularjs和openweathermap将数据从旧数组推送到新数组中
Weather forecast by pushing data from an old array into a new array using angularjs and openweathermap
大家好,我整个周末都在努力让这件事发挥作用,但我似乎无法理解。我试图实现的是使用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);
这应该行得通。
相关文章:
- csv数据中的新数组
- javascript函数,它将数组作为输入,将每个数组元素增加1,并返回增加值的新数组
- 按值名称过滤掉重复的对象,并将其放入新数组中
- 根据对多个数组唯一的元素创建一个新数组
- 使用javascript/jquery从现有数组中创建一个新数组,该数组保存项目存在的次数
- Javascript-在一个数组中搜索整个单词,并创建一个符合条件的新数组
- 使用Node中的Q promise库对值数组顺序调用/执行相同的函数;并返回带有结果的新数组/集合
- 将数组值替换为循环以创建新数组
- 带有附加元素的新数组不会可视化
- 创建新数组和清除长度之间的差异
- 如何将新数组传递给函数
- 创建新数组或增加计数器
- 将字符串数组的子字符串复制到Javascript中的新数组中
- 如何使用旧数组中的数据创建新数组
- 如何过滤/修改主干集合本身,而不是获取新数组
- 如何比较具有相同值的两个数组并将该相同值存储在新数组中
- 将现有数组的元素复制到新数组而不使用拼接
- 如何将新数组添加到另一个数组的特定对象中
- 将一个新数组放入另一个数组中
- 为什么它在创建json对象时创建数组新数组