1

I am trying to change an array in the following order:

So I have the following array:

const weekDaysArray = ['Mo', 'Tu', 'We', 'Th', 'Fr', 'Sa', 'Su']; which contains the days of the week in order, then I get a variable

const firstWeekday = Object.values(formattedReadsByDay)[0].dayOfWeek; which will return a day of the week, let's say We for the case.

So when this returns We i would like to change the array to:

weekDaysArray = ['We', 'Th', 'Fr', 'Sa', 'Su', 'Mo', 'Tu'];

I tried weekDaysArray [...weekDays.slice(-1), ...weekDays.slice(0, -1)] this but it doesn't work. Any ideeas?

4
  • There is a typo in your code? I don't see the weekDays variable where is defined. Should it be weekDaysArray? Commented May 21, 2020 at 13:11
  • No. You can ignore those, it was just an example where i'm slicing. The two variables i care about are firstWeekDay and weekDaysArray Commented May 21, 2020 at 13:16
  • Ok, in this case you should provide a more accurate example of your code and maybe what's te result you have: is it a js error? Is an array with wrong length? Is an array with correct length but wrong order?... Commented May 21, 2020 at 13:19
  • [...weekDays.slice(2), ...weekDays.slice(0,2)] Commented May 21, 2020 at 13:19

3 Answers 3

2

You need to:

  1. Find the index of the first day.
  2. Get the part of the array from the first day (inclusive) to the end. This is the new start part of the week.
  3. Get the part of the array from the beginning to the first day (exclusive). This is the new end part of the week.
  4. Join the two parts.

This can be achieved like this:

const weekDaysArray = ['Mo', 'Tu', 'We', 'Th', 'Fr', 'Sa', 'Su'];

const firstWeekday = "We";

//1. find index
const index = weekDaysArray.indexOf(firstWeekday);

//2. get start of the week
const startWeek = weekDaysArray.slice(index);
//3. get end of the week
const endWeek = weekDaysArray.slice(0, index);

//4. combine the new week
const result = [...startWeek, ...endWeek];

console.log(result);

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks. This one is super clean.
2

You can use weekDaysArray.push(weekDaysArray.shift()) That will move the first item of array to end. Like this [1, 2, 3] -> [ 2, 3, 1] Then you need to check if weekDaysArray[0] == your day, you will return array. If no, do the same operation one more time.

Comments

1

An implementation of what Dima Vak answered could be something like:

function getOrderedWeek (firstWeekDay) {
    const weekDaysArray = ['Mo', 'Tu', 'We', 'Th', 'Fr', 'Sa', 'Su'];

    for(let i = 0; i < weekDaysArray.length; i++) {
        if (weekDaysArray[0] === firstWeekDay) break;
        weekDaysArray.push(weekDaysArray.shift())
    }

    return weekDaysArray;
}

const firstWeekday = Object.values(formattedReadsByDay)[0].dayOfWeek;
console.log({
    result: getOrderedWeek(firstWeekday)
});

1 Comment

Thanks for the reply.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.