Files
30-seconds-of-code/javascript/snippets/first-n.md
2023-05-01 22:35:56 +03:00

423 B

title, type, tags, author, cover, dateModified
title type tags author cover dateModified
First n elements snippet
array
chalarangelo digital-nomad-16 2022-07-22T05:00:00-04:00

Gets the first n elements of an array.

  • Use Array.prototype.slice() with a start value of 0 and an end value of n to get the first n elements of arr.
const firstN = (arr, n) => arr.slice(0, n);
firstN(['a', 'b', 'c', 'd'], 2); // ['a', 'b']