我无法获取“日期”键的值来构建我的数组。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]. 我怎样才能解决这个问题?
1 回答
慕森卡
TA贡献1806条经验 获得超8个赞
您实际上想要第一个值,而不是第一个键。
const [dd, mm, yyyy] = Object.values(o)[0].split('/');
但是,由于您已经知道密钥的名称,您可以简单地使用o.Date
.
const [dd, mm, yyyy] = o.Date.split('/');
添加回答
举报
0/150
提交
取消