array-last icon indicating copy to clipboard operation
array-last copied to clipboard

what if use Array.slice() directly?

Open jxxl998 opened this issue 3 years ago • 0 comments

//  Get the last or last n elements in an array.

export default function (array, num = 1) {
  if (!Array.isArray)
    throw new Error('array-first expects an array as the first element');

  if (typeof num !== 'number') throw new Error('num expects a number');

  if (array.length === 0) return null;

  return num === 1 ? array.pop() : array.slice(-num);
}

jxxl998 avatar Dec 07 '22 03:12 jxxl998