qweeee
qweeee

Reputation: 63

How to move array with days of week one item backwards js

I have two arrays

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' },
];

and const daysOfWeek = ['Mon', 'Tue', 'Wed', 'Thu', 'Fri']

What I wanted to achieve is to move array(daysOfWeek) one item backwards, that is, daysOfWeek = ['Sun', 'Mon', 'Tue', 'Wed', 'Thu'].

To achieve that I created such code

const selectedDaysOfWeek = weekDays.map(({label, name}, index, array) => {
   if (daysOfWeek.includes(label)) {
       return array[index - 1].label;
   }
   return ''
});

but it returns label of undefined.

Upvotes: 0

Views: 101

Answers (4)

darklightcode
darklightcode

Reputation: 2772

You can do it with map:

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 c = weekDays.map((i, x, s) => typeof s[x - 1] === "undefined" ? s[s.length - 1].label : s[x - 1].label)

console.log(c);

Upvotes: 0

AnanthDev
AnanthDev

Reputation: 1808

You can simply use Array.prototype.pop() to remove the element from the end and return that element, then use Array.prototype.unshift() to insert an element to the beginning

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' },
];

const lastElement = weekDays.pop(); // get last element
weekDays.unshift(lastElement); // get new array that last element is first one 
let finalResult = weekDays.map(x => x.label) // get only names of week days from object
console.log(finalResult)

EDIT: according to the OP's needs

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' },
];

function getWeeks(arr) {
    return [arr[arr.length - 1], ...arr.slice(0, arr.length - 1)];
}

console.log(getWeeks(['Tue', 'Wed', 'Thu']));
console.log(getWeeks(weekDays));

Upvotes: 2

Janó Gáll
Janó Gáll

Reputation: 177

const result = [weekDays[weekDays.length-1],...weekDays.slice(0,weekDays.length-1)].slice(0,5)

Upvotes: 0

Xeelley
Xeelley

Reputation: 1116

I guess your solution is:

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 = ['Wed', 'Thu', 'Fri'] 

const firstDayIndex = weekDays.findIndex(day => day.label === daysOfWeek[0]) // get current day index
daysOfWeek.unshift(weekDays[(firstDayIndex || weekDays.length) - 1].label) // add previous day by index
daysOfWeek.pop() // remove last day
console.log(daysOfWeek) // // ['Tue', 'Wed', 'Thu'], here we go

Upvotes: 1

Related Questions