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

155
Visualizações
Empty all elements in array with pop method

I am trying to empty an array by using the pop method after that I want to push the returned result of the pop method to another array and get the full result, but the pop method doesn't satisfy my needs it always stops midway what is the problem?

let arr = [1,2,3,4,5,6,7]

let newArr = []

//console.log(arr.pop())
const fib = function(n){
   if(n <= arr.length){
     newArr.push(arr.pop())
     return fib(n+1)
   }
}

fib(0)

console.log(newArr)
about 4 years ago · Juan Pablo Isaza
3 Respostas
Responde à pergunta

0

let arr = [1,2,3,4,5,6,7]

let newArr = []

//console.log(arr.pop())
const fib = function(n){
   if(!n <= 0){
     newArr.push(arr.pop())
     return fib(n-1)
   }
}

fib(arr.length)

console.log(newArr)
about 4 years ago · Juan Pablo Isaza Relatório

0

I would do like that:

var myArr = [1,2,3,4,5,6,7,8,9];
var myNewArr = [];

while(myArr.length){
  myNewArr.push(myArr.pop());
}

console.log(myArr, myNewArr);

If you just want to reverse it you could use the reverse function for Arrays:

var myArr = [1,2,3,4,5,6,7,8,9];

myArr = myArr.reverse();

console.log(myArr);

// Or if you really need 2 variables and one needs to be empty

var myArr = [1,2,3,4,5,6,7,8,9];
var myNewArr = myArr.reverse();

myArr = [];

console.log(myArr, myNewArr);

about 4 years ago · Juan Pablo Isaza Relatório

0

If you want your array empy... I think this is the easiest way.

var array = [1,2,3,4,5,6,7]
array = []
console.log(array)
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