0

I'm using the OpenWeather API 5 day 3 hour forecast and my response JSON is giving me 40 items in an array with "dt_txt": "2018-11-22 15:00:00" as the date format of the object. My problem is that I want to render one object for EACH day (15.00 everyday) and not every 3 hour. How can I filter the array to return 1 object everyday at 15.00?

My code looks like this;

handleFormSubmit = e => {
    e.preventDefault();

    fetch(
      "https://api.openweathermap.org/data/2.5/forecast?q=" +
        this.state.userInput +
        "&units=metric&APPID="
    )
      .then(results => {
        return results.json();
      })
      .then(data => {
        let days = data.list
          .filter(day => {
            return day.dt_txt === **???????insert help here?????**;
          })
          .map((day, index) => {
            return (
              <div key={index}>
                <div className="card WeatherDays" style={{ paddingTop: 20 }}>
                  <p>{day.dt_txt.slice(5, 16)}</p>
                  <p style={pStyle}>{day.main.temp}°</p>
                  {day.main.temp >= 15 ? (
                    <i className="fas fa-sun icons fa-5x" />
                  ) : (
                    <i className="fas fa-cloud-sun-rain fa-5x" />
                  )}
                </div>
              </div>
            );
          });
        this.setState(() => {
          return {
            days: days,
            userInput: ""
          };
        });
      })
      .catch(error => {
        let errors = error;
      });
  };

1 Answer 1

2

Since dt_txt is a string , simple boolean check would suffice.

.filter(day => {
  return day.dt_txt.endsWith("15:00:00")
})
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.