繁体   English   中英

在 JavaScript 的对象数组中获取从一周的第一个日期开始的所有日期

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

需要帮助解决这个问题,应该是一个小任务,但想不通。

问题:数据是一组对象,x 为一周的第一个日期,y 为该周的值。 需要一个解决方案/函数,它将在 x 中返回该周的所有日期,并在 y 中为该特定周的每个日期返回相同的值。 我正在使用 moment.js 以备不时之需。

输入:

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

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

您可以通过使用flatMap此答案中找到的addDays扩展来做到这一点

 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)

你可以尝试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)

尝试这个!

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

好的,将数据 object 传递到 function 之上。

假设该数组的每个元素都包含一个日期,该日期始终指示要扩展的星期几

迭代所有 obj 内部数据并为每一个提取 <date,value>。 对于每个日期,迭代其他 7 个步骤(一个用于开始日期,其他 6 个用于接下来的日期,并增加一天的日期,在此之后,将其推入数组中,其值为 y。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM