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))
More Related Contents:
- Creating a JavaScript global array with static elements?
- Array.prototype.fill() with object passes reference and not new instance
- JavaScript “new Array(n)” and “Array.prototype.map” weirdness
- JavaScript: Difference between .forEach() and .map()
- Convert array to JSON
- Why can I add named properties to an array as if it were an object?
- How to extend an existing JavaScript array with another array, without creating a new array
- Is the order of elements in a JSON list preserved?
- JavaScript: How to join / combine two arrays to concatenate into one array?
- How to find if an array contains a specific string in JavaScript/jQuery?
- Sum JavaScript object propertyA values with the same object propertyB in an array of objects
- Merge 2 arrays of objects
- How to change value of object which is inside an array using JavaScript or jQuery?
- How might I extract the property values of a JavaScript object into an array?
- Sampling a random subset from an array
- Convert JavaScript array of 2 element arrays into object key value pairs
- Selecting last element in JavaScript array [duplicate]
- Comparing two arrays of objects, and exclude the elements who match values into new array in JS
- Best way to convert string to array of object in javascript?
- How to create an array in JavaScript whose indexing starts at 1?
- How to check if a string array contains one string in JavaScript? [duplicate]
- Count duplicates within an Array of Objects
- MongoDB select where in array of _id?
- Filter array of objects whose any properties contains a value
- Search array of objects for entry matching variable and check if it matches other fields
- How can I perform an inner join with two object arrays in JavaScript?
- How to get the first element of an array?
- Is array both associative and indexed?
- Javascript 2d array indexOf
- How to merge each object within arrays by index?