Search code examples
javascriptarraysjavascript-objectsweekday

Get all dates from first date of week in Array of objects in JavaScript


Need help on solving this, should be a small task, but could not figured it out.

Problem: data is an array of objects with the first date of the week in x and a value for that week in y. need a solution/function that will return all dates in that week in x and the same value in y for each date in that particular week. I'm using moment.js in case needed.

Input:

data = 
    [
        {
            "x": "2022-07-25",
            "y": 0.5
        },
        {
            "x": "2022-08-01",
            "y": 0.2
        },
        {
            "x": "2022-08-08",
            "y": 0.3
        }
    ]

Expected output:

data = 
    [
        {
            "x": "2022-07-25",
            "y": 0.5
        },
    {
            "x": "2022-07-26",
            "y": 0.5
        },
    {
            "x": "2022-07-27",
            "y": 0.5
        },
    {
            "x": "2022-07-28",
            "y": 0.5
        },
    {
            "x": "2022-07-29",
            "y": 0.5
        },
    {
            "x": "2022-07-30",
            "y": 0.5
        },
    {
            "x": "2022-07-31",
            "y": 0.5
        },
        {
            "x": "2022-08-01",
            "y": 0.2
        },
    {
            "x": "2022-08-02",
            "y": 0.2
        },
    {
            "x": "2022-08-03",
            "y": 0.2
        },
    {
            "x": "2022-08-04",
            "y": 0.2
        },
    {
            "x": "2022-08-05",
            "y": 0.2
        },
    {
            "x": "2022-08-06",
            "y": 0.2
        },
    {
            "x": "2022-08-07",
            "y": 0.2
        },
        {
            "x": "2022-08-08",
            "y": 0.3
        },{
            "x": "2022-08-09",
            "y": 0.3
        }
    ,{
            "x": "2022-08-10",
            "y": 0.3
        }
    ,{
            "x": "2022-08-11",
            "y": 0.3
        },{
            "x": "2022-08-12",
            "y": 0.3
        },{
            "x": "2022-08-13",
            "y": 0.3
        },{
            "x": "2022-08-14",
            "y": 0.3
        }
    ]

Solution

  • You can try with reduce

    const data = [{
        "x": "2022-07-25",
        "y": 0.5
      },
      {
        "x": "2022-08-01",
        "y": 0.2
      },
      {
        "x": "2022-08-08",
        "y": 0.3
      }
    ]
    
    
    const finalResult = data.reduce((result, current) => {
      const {
        x,
        y
      } = current
     
      //convert the string date to a date type
      const currentDate = new Date(x)
      
      //add the current date to the result by default
      result.push(current)
    
      const days = 6
    
      for (let day = 1; day <= days; day++) {
        //change the current date to the next date
        currentDate.setDate(currentDate.getDate() + 1)
        //add the new date data to the result
        result.push({
          x: currentDate.toISOString().substring(0, 10),
          y: y
        })
      }
    
      return result
    }, [])
    
    console.log(finalResult)