Get all array elements except for first and last

37,026

Solution 1

To achieve this you can use shift() and pop() to get the first and last elements of the array, respectively. Whatever is left in the array after those operations will be your 'middlepoints'. Try this:

var middlePoints = ['start', 'A', 'B', 'C', 'end'];
var origin = middlePoints.shift();
var destination = middlePoints.pop();

console.log(origin);
console.log(middlePoints);
console.log(destination);

shift and pop affects the current array; Is there a way to get the middlePoints changing the current array?. Like slice, but slice do not affect it

Sure, you can use filter() for that, checking the index of the item in the array:

var input = ['start', 'A', 'B', 'C', 'end'];
var output = input.filter((v, i) => i !== 0 && i !== input.length -1);

console.log(input); // original value retained
console.log(output); // copied via filter

Solution 2

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);

Solution 3

Something like this?

var allPoints = [0, 1, 2, 3, 4, 5],
    midPoints = []

/*start the iteration at index 1 instead of 0 since we want to skip the first point anyway and stop iteration before the final item in the array*/
for (var i = 1; i < (allPoints.length - 1); i++) {
  midPoints.push(allPoints[i]);
}

console.log(midPoints);

Share:
37,026

Related videos on Youtube

Vilius
Author by

Vilius

Updated on July 18, 2022

Comments

  • Vilius
    Vilius almost 2 years

    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?

  • Thomas
    Thomas over 6 years
    Although this generates the correct output, I'd consider this way too cumbersome and explicit as it tells JS every tiny little step that it has to execute. Using an explicit loop for this task is an antipattern to me. Don't micromanage. JS's built in midPoints = allPoints.slice(1, allPoints.length-1) does the same, is more readable, more clear about your intention and even faster.
  • Tom O.
    Tom O. over 6 years
    Was the down-vote necessary? This is a valid solution to OPs question. You make it sounds like a basic for-loop is going to destroy performance. I agree that, yes, it would make more sense to use some built in js array functions - but this is a very simple solution and it works.
  • Thomas
    Thomas over 6 years
    The down-vote reflects my opinion that this ain't a good solution for the question, even if it produces the correct output. And it's not about the performance, it's about that this code has not advantages whatsoever over using the built in method; not even as little as the strawman I often hear "But my explicit code is faster". And my point is that this code is the exact opposite of simple. You're micromanaging. Moving items from one array to another and complicate/obscure the actual task at hand: give me the part of allpoints starting after the first item and ending before the last item.
  • Prabhakar
    Prabhakar over 5 years
    can be improved a bit more by replacing line var middle = arr.slice(1, arr.length-1); with var middle = arr.slice(1, -1);
  • ValRob
    ValRob over 5 years
    shift and pop affects the current array; Is there a way to get the middlePoints changing the current array?. Like slice, but slice do not affect it(?)