Files
30-seconds-of-code/snippets/js/s/last-n.md
2023-05-07 16:07:29 +03:00

408 B

title, type, language, tags, author, cover, dateModified
title type language tags author cover dateModified
Last n elements snippet javascript
array
chalarangelo interior-5 2022-07-23T05:00:00-04:00

Gets the last n elements of an array.

  • Use Array.prototype.slice() with a start value of -n to get the last n elements of arr.
const lastN = (arr, n) => arr.slice(-n);
lastN(['a', 'b', 'c', 'd'], 2); // ['c', 'd']