Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

126
Views
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 answers
Answer question

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 Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!