Can anybody explain this code snippet to me?

Tell us what’s happening:
Describe your issue in detail here.

The [0] is to avoid object key duplicates? How does filter work in this case?

daysOfWeek = [
    { day: "Wednesday", temp: 10, date: "2021-11-03 09:00:00" },
    { day: "Wednesday", temp: 2, date: "2021-11-03 01:00:00" },
    { day: "Wednesday", temp: 7, date: "2021-11-03 18:00:00" },
    { day: "Sunday", temp: 20, date: "2021-11-07 15:00:00" },
    { day: "Sunday", temp: 6, date: "2021-11-07  5:00:00" }
  ];

let holderArray = [
    { day: "Sunday", rows: [] },
    { day: "Monday", rows: [] },
    { day: "Tuesday", rows: [] },
    { day: "Wednesday", rows: [] },
    { day: "Thursday", rows: [] },
    { day: "Friday", rows: [] },
    { day: "Saturday", rows: [] }
  ];

 daysOfWeek.forEach((item) => {
    let row = holderArray.filter((obj) => {
      return obj.day === item.day;
    })[0];
    row.rows.push({ temp: item.temp, date: item.date });
  });


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:

What is the return value of the filter array method? I’ll give you a hint, we are dealing with arrays here :slight_smile: The filter on the holderArray will return an array with only one element in it, so [0] is just getting the value in that returned array. If you aren’t sure what that value is you can put a console.log(row) right after the filter.

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.