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

131
Vistas
Reduce length of an array by 1

I have an array of images and I just need an function that decreases the length of this array by 1 every time it's called. I've tried doing it with .reduce() and just trying to set the old array as array - 1 but nothing works as expected.

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

0

There are multiple options available.

  • pop() will remove the last element from the array and return it
  • decreasing the length by one will just remove the last element
  • use splice() method

const array = [1, 2, 3, 4, 5];
console.log(array);
// use pop which retrieves the last element from the array and removes it from the array
const lastElement = array.pop();
console.log(`Last element ${lastElement} was removed from the array`);
console.log(array);
// you can also simply decrease the length by one
array.length--;
console.log(array);
// you could also use splice()
array.splice(-1);
console.log(array);
.as-console-wrapper { max-height: 100% !important; top: 0; }

Abovementioned methods work on the original array. If you want to instead get a new array (i.e. a copy) with the last element removed use slice():

const array = [1, 2, 3, 4, 5];
const newArray = array.slice(0, -1);
console.log(array);
console.log(newArray);
.as-console-wrapper { max-height: 100% !important; top: 0; }

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