This is the example of the array
arr = [1,2,3,4,5]; I expect to have this kind of result
arr = [5]; 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}); arr = arr.slice(arr.length - 1); would be the better choice.arr = arr.slice(-1) would do.
arr = [arr.pop()];?slicemethod :arr.slice(arr.length - 1)