Files
30-seconds-of-code/snippets/first-n.md
Angelos Chalaris 61200d90c4 Kebab file names
2023-04-27 21:58:35 +03:00

404 B

title, tags, author, cover, firstSeen
title tags author cover firstSeen
First n elements 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']