Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

115
Visualizações
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 Respostas
Responde à pergunta

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 Relatório

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 Relatório

0

const result = [weekDays[weekDays.length-1],...weekDays.slice(0,weekDays.length-1)].slice(0,5)
about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda