By Panda


2020-02-14 08:27:20 8 Comments

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 comments

@PrakashT 2020-02-14 08:30:47

this.forecast.splice(0,1);

or

this.forecast.shift()

for more clarification see this link

Now first index removed from your forecast array.

@Panda 2020-02-14 08:33:36

it replace the sunday to tuesday

@Panda 2020-02-14 08:35:08

{...friday},{...saturday}, {...sunday}, {...monday}, {...tuesday} output. but it remove the saturday to tuesday instead it will remove the friday

@PrakashT 2020-02-14 08:38:41

@panda i uppdated my code.Now it will work

@Neji Soltani 2020-02-14 08:40:59

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"]

@Panda 2020-02-14 08:39:34

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

Related Questions

Sponsored Content

95 Answered Questions

[SOLVED] How to remove specific item from array?

  • 2011-04-23 22:17:18
  • Walker
  • 6348602 View
  • 7931 Score
  • 95 Answer
  • Tags:   javascript arrays

37 Answered Questions

[SOLVED] How do I return the response from an asynchronous call?

62 Answered Questions

[SOLVED] How to replace all occurrences of a string?

4 Answered Questions

86 Answered Questions

[SOLVED] How do JavaScript closures work?

57 Answered Questions

[SOLVED] How do I check if an element is hidden in jQuery?

58 Answered Questions

[SOLVED] How do I include a JavaScript file in another JavaScript file?

86 Answered Questions

[SOLVED] How do I make the first letter of a string uppercase in JavaScript?

42 Answered Questions

[SOLVED] How do I remove a property from a JavaScript object?

58 Answered Questions

[SOLVED] How do I redirect to another webpage?

Sponsored Content