Looping through a JS array of objects and linking it to an external array not working

Tell us what’s happening:
Hi, guys. I’m trying to link days of the week using the Date object from forecasts to the days array but when the day property’s value in forecasts is a duplicate, it skips that day and daysOfWeek is left with only one object for each day.

const linkDays = () => {
  let daysOfWeek = {};
  let days = [
    "Sunday",
    "Monday",
    "Tuesday",
    "Wednesday",
    "Thursday",
    "Friday",
    "Saturday"
  ];
  let forecasts = [
    { temp: 20, day: "2021-11-03 09:00:00" },
    { temp: 23, day: "2021-11-03 09:00:00" }, 
    { temp: 24, day: "2021-11-07 15:00:00" },
    { temp: 24, day: "2021-11-07  5:00:00" },
    { temp: 24, day: "2021-11-07 15:00:00" },
    { temp: 12, day: "2021-11-03 18:00:00" }
  ];

  let dayIndex = new Date(forecasts[0].day).getDay();
  daysOfWeek[days[dayIndex]] = [forecasts[0].day];
  dayIndex = new Date(forecasts[1].day).getDay();
  daysOfWeek[days[dayIndex]] = [forecasts[1].day];
  dayIndex = new Date(forecasts[2].day).getDay();
  daysOfWeek[days[dayIndex]] = [forecasts[2].day];
};

linkDays();

```jsx

const JSX = <div></div>;

Your browser information:

User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/95.0.4638.54 Safari/537.36

Challenge: Create a Simple JSX Element

Link to the challenge:

That’s not entirely true, what happens is that the value have the same key, so when writing them in the daysOfWeek object that key gets overwritten with the latest value.

Hope this helps. :slight_smile:

Thank you, Marmiz. So, in this case, it’s just not possible to have objects with the same keys? How can I link two objects from wednesday to a wednesday key?

With the above code, you are using one of the days value as key, so you have only 7 possible values.

But yet again, I have no idea about your requirements or your constrains to give you a proper suggestion.

The only thing that’s weird is your data quality:

    { temp: 20, day: "2021-11-03 09:00:00" },
    { temp: 23, day: "2021-11-03 09:00:00" }, 

How can you have two different temperature for the exact same day and time?

1 Like

I just corrected the data issue. Now I have :

let forecasts = [
    { temp: 20, date: "2021-11-03 09:00:00" },
    { temp: 23, date: "2021-11-03 01:00:00" },
    { temp: 12, date: "2021-11-03 18:00:00" },
    { temp: 24, date: "2021-11-07 15:00:00" },
    { temp: 24, date: "2021-11-07  5:00:00" },
    { temp: 24, date: "2021-11-07 15:00:00" }
  ];

And I want to convert it to:

daysOfWeek = [
    { day: "Wednesday", temp: 20, date: "2021-11-03 09:00:00" },
    { day: "Wednesday", temp: 23, date: "2021-11-03 01:00:00" },
    { day: "Wednesday", temp: 12, date: "2021-11-03 18:00:00" }
  ];

Is it possible?

It is absolutely possible :slight_smile:

At the core all you want to do is loop over the forecast array and return a new array with “different” data.

I’ll let you figure out how to do it yourselves, but my go to in this case would be using Array.map.

Hope this helps :slight_smile: