我无法获取“日期”键的值来构建我的数组。
const input = [{
"Date": "12/08/2020",
"Day": "Wednesday"
}, {
"Date": "13/08/2020",
"Day": "Thursday"
}, {
"Date": "14/08/2020",
"Day": "Friday"
}];
function get(o, days) {
const weekdays = ['Sunday', 'Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday'];
const [dd, mm, yyyy] = Object.keys(o)[0].split('/');
const date = new Date(`${yyyy}-${mm}-${dd}`);
date.setUTCDate(date.getUTCDate() + days);
const key = `${
`${date.getUTCDate()}`.padStart(2, '0')
}/${
`${(date.getUTCMonth() + 1)}`.padStart(2, '0')
}/${
date.getUTCFullYear()
}`;
const value = weekdays[date.getUTCDay()];
return {
[key]: value
};
}
function prepend(array, count) {
while (count-- > 0) {
array.unshift(get(input[0], -1));
}
}
function append(array, count) {
while (count-- > 0) {
array.push(get(input[input.length - 1], 1));
}
}
prepend(input, 1);
append(input, 1);
console.log(input);
控制台显示此输出:
{NaN/NaN/NaN: undefined},{Date: "12/08/2020", Day: "Wednesday"},{Date: "13/08/2020", Day: "Thursday"},{Date: "14/08/2020", Day: "Friday"},{NaN/NaN/NaN: undefined}
似乎问题出在Object.keys(o)[0]. 我怎样才能解决这个问题?
慕森卡
相关分类