What is the best way to get only the first and last value in Array?
If arr = [0,1,2,3]
Result: arr = [0,3]
If arr = [0]
Result: arr = [0]
If arr = []
Expect result: arr = []
I could get in this way.
[arr[0], arr[arr.length - 1]]
But I want to know if there is a better way to get.
arr.splice(1, -1)[].arr.reduce((acc,curr,i)=>i===0||i===arr.length-1?[...acc,curr]:acc,[])