我无法获取'Date'键的值来构建数组。
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]
上。我该如何解决?
答案 0 :(得分:1)
您实际上是想要第一个值,而不是第一个键。
const [dd, mm, yyyy] = Object.values(o)[0].split('/');
但是,由于您已经知道密钥的名称,因此只需使用o.Date
。
const [dd, mm, yyyy] = o.Date.split('/');