0
votes

here's the code.

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

what I want is to remove the first weather in weather forecast. for example.

friday, saturday, sunday, monday, tuesday, wednesday

it will remove the saturday. it only display the sunday to tuesday. enter image description here

3

3 Answers

1
votes
this.forecast.splice(0,1);

or

this.forecast.shift()

for more clarification see this link

Now first index removed from your forecast array.

1
votes

You should use splice

this.forecast.splice(0, 1)

where 0 is the index position and 1 the number of element to remove

example

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

return this.forecast.shift();

to remove the first weather in forecast.

you can use the shift() - method removes the first element from an array and returns that removed.

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