From 8dfca3aa91043fdf3d23c97e7df110b146b51c1c Mon Sep 17 00:00:00 2001 From: Angus MacDonald Date: Fri, 27 Nov 2020 14:07:00 -0600 Subject: [PATCH] Add arrayf.removeFirstInstance --- src/arrayf/arrayf.ts | 17 +++++++++++++++-- 1 file changed, 15 insertions(+), 2 deletions(-) diff --git a/src/arrayf/arrayf.ts b/src/arrayf/arrayf.ts index c53ee4696a..5b4214620a 100644 --- a/src/arrayf/arrayf.ts +++ b/src/arrayf/arrayf.ts @@ -28,5 +28,18 @@ export class arrayf { return output; } - -} \ No newline at end of file + /** + * Removes the first instance of the value from the given array. + * Not a complex function but `removeFirstInstance` is more readable than + * the function contents. + * ``` + * const values = [1, 1, 2, 3, 5, 8]; + * removeFirstInstance(values, 1); // Returns [1, 2, 3, 5, 8] + * ``` + */ + static removeFirstInstance(values: T[], value: T): T[] { + return [ + ...values.slice(0, values.indexOf(value)), + ...values.slice(values.indexOf(value) + 1)]; + } +}