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

198
Visualizações
Removing the first element in an array without using array methods

So I have homework that requires me to dequeue from an array like so:

let arr = [1, 2, 3]

My only problem is that I'm restricted in using any array method except for the .length method.

I have tried using the delete to remove the first element but I know it doesn't adjust the array and reflects undefined for the first element:

function dequeue(){
  delete arr[0];
  console.log(arr)
}

Any help or advice on how to do it is highly appreciated.

about 4 years ago · Juan Pablo Isaza
3 Respostas
Responde à pergunta

0

Assuming, you can still use assigment with index, you could copy all items one index before and decrement the length of the array.

let array = [1, 2, 3];

for (let i = 1; i < array.length; i++) array[i - 1] = array[i];

array.length--;

console.log(array);

about 4 years ago · Juan Pablo Isaza Relatório

0

If .map is allowed to use in your case.

array = array.map((elm,index,arr) => arr[index+1]);
array.length--;
about 4 years ago · Juan Pablo Isaza Relatório

0

You can use Destructuring assignment. try this out:

const [firstElement, ...restOfTheArray] = yourArray;

restOfTheArray is the same array with the first element removed.

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