-2

This is the example of the array

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

I expect to have this kind of result

arr = [5]; 
8
  • And what have you tried? Commented Apr 24, 2017 at 9:52
  • Please read and apply the following instructions How to ask a good question and How to create a Minimal, Complete, and Verifiable example to your question! If you improve your question, you improve your chances for us to help you! Commented Apr 24, 2017 at 9:53
  • arr = [arr.pop()];? Commented Apr 24, 2017 at 9:53
  • you can use slice method : arr.slice(arr.length - 1) Commented Apr 24, 2017 at 9:53
  • thank you for the advice, in my next question in the future I will follow your advice @DouwedeHaan Commented Apr 24, 2017 at 10:04

1 Answer 1

12

Use Splice/Slice function as below:

var sliceArr = [1, 2, 3, 4, 5]; sliceArr = sliceArr.slice(-1); console.log({sliceArr}); // OR var spliceArr = [1, 2, 3, 4, 5]; spliceArr = spliceArr.splice(spliceArr.length - 1, 1); console.log({spliceArr});

Sign up to request clarification or add additional context in comments.

4 Comments

arr = arr.slice(arr.length - 1); would be the better choice.
@str, yes you can also use it
Just arr = arr.slice(-1) would do.
thank you for your answer it really works!!

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.