Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

114
Vistas
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.

about 4 years ago · Juan Pablo Isaza
3 Respuestas
Responde la pregunta

0

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));

about 4 years ago · Juan Pablo Isaza Denunciar

0

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
about 4 years ago · Juan Pablo Isaza Denunciar

0

const result = [weekDays[weekDays.length-1],...weekDays.slice(0,weekDays.length-1)].slice(0,5)
about 4 years ago · Juan Pablo Isaza Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda