【问题标题】:How to remove the first weather in weather forecast using weather api如何使用天气api删除天气预报中的第一个天气
【发布时间】:2020-05-30 00:43:53
【问题描述】:

这是代码。

setWeatherForecast(unit: any, scaleTemp: any) {
    this.forecast.splice(0, this.forecast.length);
    this.weatherService.getLocation().subscribe(data => {
      this.lat = JSON.parse(data['_body']).latitude;
      this.lon = JSON.parse(data['_body']).longitude;

      this.weatherService
        .fiveDayForecast(this.lat, this.lon, unit)
        .subscribe(forecastData => {
          for (let i = 0; i < forecastData.list.length; i = i + 8) {
            console.log(forecastData.list[i]);
            const forecastWeather = new Forecast(
              forecastData.city.name,
              forecastData.list[i].weather[0].description,
              forecastData.list[i].main.temp.toFixed(0) + scaleTemp,
              forecastData.list[i].dt_txt.replace(/\s/, 'T'),
              forecastData.list[i].weather[0].icon
            );
            this.forecast.push(forecastWeather);
          }
          return this.forecast;
        });
    });
  }

我想要的是删除天气预报中的第一个天气。 例如。

周五、周六、周日、周一、周二、周三

它将删除星期六。它只显示星期天到星期二。

【问题讨论】:

    标签: javascript typescript weather-api


    【解决方案1】:
    this.forecast.splice(0,1);
    

    this.forecast.shift()
    

    有关更多说明,请参阅link

    现在从您的预测数组中删除第一个索引。

    【讨论】:

    • 它取代了星期日到星期二
    • {...friday},{...saturday}, {...sunday}, {...monday}, {...tuesday} 输出。但它会删除星期六到星期二,而是会删除星期五
    【解决方案2】:

    你应该使用拼接

    this.forecast.splice(0, 1)
    

    其中 0 是索引位置,1 是要删除的元素数

    例子

    list=["bar", "baz", "foo", "qux"]
    list.splice(2, 1)
    // Starting at index position 2, remove one element
    ["bar", "baz", "qux"]
    

    【讨论】:

      【解决方案3】:

      return this.forecast.shift();

      删除预测中的第一个天气。

      您可以使用shift() - 方法从数组中删除第一个元素并返回已删除的元素。

      https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/shift

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2011-11-15
        • 2011-05-16
        • 1970-01-01
        • 2016-01-27
        • 2018-09-17
        • 2013-06-22
        • 2016-06-10
        • 1970-01-01
        相关资源
        最近更新 更多