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

158
Vistas
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 Respuestas
Responde la pregunta

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 Denunciar

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 Denunciar

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