Home > front end >  Get all dates from first date of week in Array of objects in JavaScript
Get all dates from first date of week in Array of objects in JavaScript

Time:09-06

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

CodePudding user response:

You could do this by using flatMap and an extension for addDays found in this answer

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

Date.prototype.addDays = function(days) {
    var date = new Date(this.valueOf());
    date.setDate(date.getDate()   days);
    return date;
}

const result = data.flatMap(item => {
  
  const date = new Date(item.x)
  
  return [0,1,2,3,4,5,6,7].map(d => (
    {
       x: date.addDays(d).toISOString(), 
       y: item.y
    }       
  ));
})

console.log(result)

CodePudding user response:

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)

CodePudding user response:

Try this!

const expandEveryWeek = function(data){
    //Array for result
    const result = [];

    //Iterate all array parameters (Assuming first element is the first of week)
    //It's always Monday
    for(const obj of data){
        const {x,y} = obj; //Extract values (x,y)
        const date = new Date(x); //Create date with x
        for(let j=0;j<7;j  ){
            result.push({ x : date.toISOString().substring(0,10), y}); //Insert first (0 index)
            date.setDate(date.getDate()   1) //Add day
        }
    }
    return result;
}

Ok so, pass data object to above function.

The assumption is that each element of this array contains a date that always indicates the Monday of the week to expand

Iterate all obj inside data and for every one extract <date,value>. For every date, iterate other 7 step (one for the start date and other 6 for following days and increment date of one single day, after this, push it inside array with his value y.

  • Related