From c31c21fd7091215abbd2e2c07b7aa1ec38f2d4bd Mon Sep 17 00:00:00 2001 From: DazFather <39210887+DazFather@users.noreply.github.com> Date: Wed, 30 Mar 2022 00:46:10 +0200 Subject: [PATCH] Improve first and last functions using slice MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Great video, keep up with the great work man ❤ --- arrayUtils/arrayUtils.js | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/arrayUtils/arrayUtils.js b/arrayUtils/arrayUtils.js index 1ab7a87..dcca903 100644 --- a/arrayUtils/arrayUtils.js +++ b/arrayUtils/arrayUtils.js @@ -2,12 +2,12 @@ import { randomNumberBetween } from "../other.js" export function first(array, n = 1) { if (n === 1) return array[0] - return array.filter((_, index) => index < n) + return array.slice(0, n) } export function last(array, n = 1) { if (n === 1) return array[array.length - 1] - return array.filter((_, index) => array.length - index <= n) + return array.slice(-n) } export function sample(array) { @@ -16,6 +16,7 @@ export function sample(array) { export function pluck(array, key) { return array.map(element => element[key]) + .filter(element => element !== undefined) } export function groupBy(array, key) {