Nest all content into snippets

This commit is contained in:
Angelos Chalaris
2023-05-07 16:07:29 +03:00
parent 2ecadbada9
commit 6a45d2ec07
1240 changed files with 0 additions and 0 deletions

21
snippets/js/s/first-n.md Normal file
View File

@ -0,0 +1,21 @@
---
title: First n elements
type: snippet
language: javascript
tags: [array]
author: chalarangelo
cover: digital-nomad-16
dateModified: 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`.
```js
const firstN = (arr, n) => arr.slice(0, n);
```
```js
firstN(['a', 'b', 'c', 'd'], 2); // ['a', 'b']
```