How do I get the last 5 elements, excluding the first element from an array?

JavascriptArrays

Javascript Problem Overview


In a JavaScript array, how do I get the last 5 elements, excluding the first element?

[1, 55, 77, 88] // ...would return [55, 77, 88]

adding additional examples:

[1, 55, 77, 88, 99, 22, 33, 44] // ...would return [88, 99, 22, 33, 44]

[1] // ...would return []

Javascript Solutions


Solution 1 - Javascript

You can call:

arr.slice(Math.max(arr.length - 5, 1))

If you don't want to exclude the first element, use

arr.slice(Math.max(arr.length - 5, 0))

Solution 2 - Javascript

Here is one I haven't seen that's even shorter

arr.slice(1).slice(-5)

Run the code snippet below for proof of it doing what you want

var arr1 = [0, 1, 2, 3, 4, 5, 6, 7],
  arr2 = [0, 1, 2, 3];

document.body.innerHTML = 'ARRAY 1: ' + arr1.slice(1).slice(-5) + '<br/>ARRAY 2: ' + arr2.slice(1).slice(-5);

Another way to do it would be using lodash https://lodash.com/docs#rest - that is of course if you don't mind having to load a huge javascript minified file if your trying to do it from your browser.

_.slice(_.rest(arr), -5)

Solution 3 - Javascript

If you are using lodash, its even simpler with takeRight.

_.takeRight(arr, 5);

Solution 4 - Javascript

var y = [1,2,3,4,5,6,7,8,9,10];

console.log(y.slice((y.length - 5), y.length))

you can do this!

Solution 5 - Javascript

Try this:

var array = [1, 55, 77, 88, 76, 59];
var array_last_five;
array_last_five = array.slice(-5);
if (array.length < 6) {
     array_last_five.shift();
}

Solution 6 - Javascript

ES6 way:

I use destructuring assignment for array to get first and remaining rest elements and then I'll take last five of the rest with slice method:

const cutOffFirstAndLastFive = (array) => {
  const [first, ...rest] = array;
  return rest.slice(-5);
}

cutOffFirstAndLastFive([1, 55, 77, 88]);

console.log(
  'Tests:',
  JSON.stringify(cutOffFirstAndLastFive([1, 55, 77, 88])),
  JSON.stringify(cutOffFirstAndLastFive([1, 55, 77, 88, 99, 22, 33, 44])),
  JSON.stringify(cutOffFirstAndLastFive([1]))
);

Solution 7 - Javascript

You can do it in one line like this:

const y = [1,2,3,4,5,6,7,8,9,10];
const lastX = 5;
const res = y.filter((val, index, arr) => index > arr.length - lastX - 1);
    
console.log(res);

.filter((val, index, arr) => index > arr.length - 6)

Solution 8 - Javascript

Beginner solution:

var givme = function(n) {
    if(n.length == 1) {
        return [];
    }
    if(n.length > 5) {
        return n.slice(n.length-5, n.length);
    }
    if(n.length <= 5) {
       return n.slice(1, n.length);
    }
}

// console.log(givme([1, 55, 77, 88, 99, 22, 33, 44]));

Solution 9 - Javascript

array.reverse()
     .slice(0,5)
     .reverse()  //if you wanna keep the order of last 5

const myOriginalArray = [...Array(10).keys()] //making an array of numbers

const instanceFromlastFiveItemsOfMyArray = [
    ...myOriginalArray.reverse().slice(0,5).reverse()
    ]

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionTIMEXView Question on Stackoverflow
Solution 1 - JavascriptSLaksView Answer on Stackoverflow
Solution 2 - JavascriptBelldanduView Answer on Stackoverflow
Solution 3 - JavascripthhsadiqView Answer on Stackoverflow
Solution 4 - JavascriptMadhu KumarView Answer on Stackoverflow
Solution 5 - JavascriptArkaView Answer on Stackoverflow
Solution 6 - JavascriptgodblessstrawberryView Answer on Stackoverflow
Solution 7 - JavascriptNoel SchenkView Answer on Stackoverflow
Solution 8 - JavascriptjsduniyaView Answer on Stackoverflow
Solution 9 - JavascriptNima MalaeriView Answer on Stackoverflow