-3

This is the example of the array

arr = [1,2,3,4,5];

I expect to have this kind of result

arr = [5];
Remigijus G.
  • 27
  • 1
  • 10
MariaJen
  • 1,065
  • 1
  • 8
  • 17

1 Answers1

11

Use Splice/Slice function as below:

var arr = [1, 2, 3, 4, 5];
arr = arr.slice(-1);
console.log(arr);

OR

var arr = [1, 2, 3, 4, 5];
arr = arr.splice(arr.length - 1,1);
console.log(arr);
str
  • 42,689
  • 17
  • 109
  • 127