You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

JavaScript:如何将星期数组中的每个元素转换为前一天?现有代码结果不符合预期

Shift Each Weekday in Array to Its Previous Day in JavaScript

I've written the following JavaScript code to shift each weekday in my array to the previous day, but it's not working as expected:

const weekDays = [ 
  { label: 'Mon', name: 'Monday' }, 
  { label: 'Tue', name: 'Tuesday' }, 
  { label: 'Wed', name: 'Wednesday' }, 
  { label: 'Thu', name: 'Thursday' }, 
  { label: 'Fri', name: 'Friday' }, 
  { label: 'Sat', name: 'Saturday' }, 
  { label: 'Sun', name: 'Sunday' }, 
]; 
let daysOfWeek = ['Tue','Thu', 'Sun'] 
const firstDayIndex = weekDays.findIndex(day => day.label === daysOfWeek[0]) // 获取当前日期索引
daysOfWeek.unshift(weekDays[(firstDayIndex || weekDays.length) - 1].label) // 通过索引添加前一天
daysOfWeek.pop() // 删除最后一个元素
console.log(daysOfWeek)

Right now, the output is ["Mon", "Tue", "Thu"], but I want ["Mon", "Wed", "Sat"] — meaning every element in daysOfWeek should be replaced with the weekday that comes right before it (e.g., Tue → Mon, Thu → Wed, Sun → Sat). How can I fix this code to achieve the desired result?


Solution

The issue with your current code is that you're only adjusting the first element in the array and removing the last one, instead of processing every element to shift it to the previous day. Here's how you can fix this:

  1. Create a helper object to map each weekday label to its index in the weekDays array — this makes index lookups far faster than repeating findIndex calls in a loop.
  2. Iterate over each element in daysOfWeek, calculate the index of its previous day, and retrieve the corresponding label.
  3. Handle the edge case where the previous day of "Mon" is "Sun" (since we need to loop around the week).

Here's the revised code:

const weekDays = [ 
  { label: 'Mon', name: 'Monday' }, 
  { label: 'Tue', name: 'Tuesday' }, 
  { label: 'Wed', name: 'Wednesday' }, 
  { label: 'Thu', name: 'Thursday' }, 
  { label: 'Fri', name: 'Friday' }, 
  { label: 'Sat', name: 'Saturday' }, 
  { label: 'Sun', name: 'Sunday' }, 
]; 

// Create a lookup map for quick index retrieval
const dayIndexMap = weekDays.reduce((map, day, index) => {
  map[day.label] = index;
  return map;
}, {});

let daysOfWeek = ['Tue','Thu', 'Sun'];

// Process each day to get its previous day
daysOfWeek = daysOfWeek.map(dayLabel => {
  const currentIndex = dayIndexMap[dayLabel];
  // Calculate previous index: if current is 0 (Mon), wrap to 6 (Sun), else subtract 1
  const previousIndex = currentIndex === 0 ? weekDays.length - 1 : currentIndex - 1;
  return weekDays[previousIndex].label;
});

console.log(daysOfWeek); // Output: ["Mon", "Wed", "Sat"]

Explanation:

  • Lookup Map: The dayIndexMap object stores each weekday label as a key and its index in weekDays as the value. This avoids redundant findIndex calls, making the code more efficient (especially if your array grows larger).
  • Mapping Each Day: Using Array.map() lets us transform every element in daysOfWeek individually. For each day, we find its current index, compute the previous index (handling the wrap-around for Monday), and grab the corresponding label from weekDays.
  • Edge Case Handling: When the current day is Monday (index 0), we set the previous index to weekDays.length - 1 (which is 6, the index for Sunday) instead of -1, which would be invalid.

This approach ensures every element in your array gets shifted to the previous weekday, exactly as you need.

内容的提问来源于stack exchange,提问作者qweeee

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.04.30 18:57:42