对象到对象数组

时间:2019-06-13 13:29:42

标签: javascript arrays

我想根据周数创建一个数组列表。因此,天数除以7就可以得到星期,并使用日期进行锻炼就可以确定一周中第一天的日期。

const data = {
    startDate: '2019-06-10',
    days: 14
    }

到..

const myObject = {
    Weeks: [
        {
            'Week 1': {
                start: '2019-06-10',
                end: '2019-06-07',
                days: {
                    0: {},
                    1: {},
                    2: {},
                    3: {},
                    4: {},
                    5: {},
                    6: {}
                }
            },

                'Week 2': {
                start: '2019-06-20',
                end: '2019-06-27',
                days: {
                    0: {},
                    1: {},
                    2: {},
                    3: {},
                    4: {},
                    5: {},
                    6: {}
                }
            },
        }
    ]
};

到目前为止我尝试过的...

const numberOfWeeks = data.days /7;


const weeks = new Array(numberOfWeeks).map(numberOfWeeks => weeks[numberOfWeeks] = new Array(7));

2 个答案:

答案 0 :(得分:0)

您可以执行以下操作。

const data = {
    startDate: '2019-06-10',
    days: 14
}

let numberOfDays = parseInt(data.days / 7);
let remainingDays = parseInt(data.days % 7);

var previousDate = new Date(data.startDate);
previousDate.setMonth(previousDate.getMonth() + 1);

let object = {
    Weeks: new Array(numberOfDays + (remainingDays ? 1 : 0)).fill({})
        .map((week, index) => {
            let newWeek = {};
            let startDate = new Date(previousDate);

            let endDate = new Date(startDate);
            endDate.setDate(endDate.getDate() + 6);

            newWeek['Week ' + (index + 1)] = {
                start: `${startDate.getFullYear()}-${startDate.getMonth()}-${startDate.getDate()}`,
                end: `${endDate.getFullYear()}-${endDate.getMonth()}-${endDate.getDate()}`,
                days: {
                    ...new Array(7)
                        .fill(new Date(startDate))
                        .map((date, index) => {
                            date.setDate(startDate.getDate() + index);
                            return `${date.getFullYear()}-${date.getMonth()}-${date.getDate()}`
                        })
                }

            };
            previousDate = endDate;
            previousDate.setDate(previousDate.getDate() + 1);

            return newWeek;

        })
};

console.log(JSON.stringify(object, null, 2));

解决方案中没有什么要注意的-
 1.将String Date转换为Date对象,我们需要在月份中添加1,默认情况下,JS Date Object中的Month从0开始。
 2.我还添加了该功能,以考虑到溢出天数,例如计算周数。 16天的结果变成3周。您可以通过为 remainingDays 变量

分配0来将其删除

答案 1 :(得分:0)

我认为这是一种对您有用的方法:

const data = {
  startDate: '2019-06-10',
  days: 28
}

function getWeeksDetail(data) {
  const weeks = [];

  for (let i = 0; i < (data.days / 7); i += 1) {
    weeks.push({
      [`Week_${i+1}`]: (function(data) {
        let o = {
          start: null,
          end: null,
          days: []
        };

        for (let j = 0; j < 7; j += 1) {
          let d = new Date(data.startDate);
          d = new Date(d.setDate(d.getDate() + (i * 7) + j));
          if (j === 0) {
            o.start = `${d.getMonth() + 1}/${d.getDate() + 1}/${d.getFullYear()}`
          }
          if (j === 6) {
            o.end = `${d.getMonth() + 1}/${d.getDate() + 1}/${d.getFullYear()}`
          }
          o.days.push({
            [j]: `${d.getMonth() + 1}/${d.getDate() + 1}/${d.getFullYear()}`
          });
        }

        return o;

      })(data)
    })
  }

  return weeks;
}

console.log(getWeeksDetail(data))