I have an array of locations, I need to be able to access origin, middlepoints and destination separately.
I know that my origin is always the first element and the destination is always the last element but I can't figure out how can I dynamically can access all the middlepoints?
You can use array. slice(0,1) // First index is removed and array is returned. FIrst index is not removed, a copy is created without the first element.
The shift() method removes the first element from an array and returns that removed element. This method changes the length of the array.
To remove the first and last elements from an array, call the shift() and pop() methods on the Array. The shift method removes the first and the pop method removes the last element from an array.
Since there is no data I am taking a basic array to show. Also by this method you will preserve your original array.
var arr = [1,2,3,4,5,6,7];
var middle = arr.slice(1, -1);
console.log(middle);
OR
var arr = [1,2,3,4,5,6,7];
var middle = arr.slice(1, arr.length-1);
console.log(middle);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With