Skip to content

array-handling functions that search for instances of a value and remove them

License

Notifications You must be signed in to change notification settings

writetome51/array-remove-all-of-first-of

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

14 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

removeFirstOf<T>(
     value: T,
     array: T[]
): void

Removes the first instance of value from array.

removeAllOf<T>(
     value: T,
     array: T[]
): void

Removes all instances of value from array.

removeAllOfEach<T>(
     values: T[],
     array: T[]
): void

Removes all instances of each value in values from array.

All the functions use identical === to find matches, except when the value being search for is an array. Arrays must only have identical content to be a match (see examples).

Examples

let arr = ['a', 'b', 'c', 'a', 'b', 'c'];
removeFirstOf('c', arr);
// arr is now ['a', 'b', 'a', 'b', 'c']

arr = ['a', 'b', 'c', 'a', 'b', 'c'];
removeAllOf('c', arr);
// arr is now ['a', 'b', 'a', 'b']

arr = ['a', 'b', 'c', 1, 2, 3, 'a', 'b', 'c'];
removeAllOfEach(['a', 'b', 'c'], arr);
// arr is now [1, 2, 3]

const obj = {name: 'steve'};
arr = [obj, {name: 'steve'}];
removeAllOf(obj, arr);
// arr is now [{name: 'steve'}]

// All the functions use identical `===` to find matches, except when the 
// value being search for is an array. Arrays must only have identical 
// content to be a match:

arr = [[1,2], [3,4], [1,2]];
removeFirstOf([1,2], arr);
// arr is now [ [3,4], [1,2] ]

const obj = {name: 'steve'};
arr = [ [obj], [{name: 'steve'}] ];
removeAllOf([obj], arr);
// arr is now [ [{name: 'steve'}] ]

Installation

npm i @writetome51/array-remove-all-of-first-of'

Loading

import {removeFirstOf, removeAllOf, removeAllOfEach} 
    from '@writetome51/array-remove-all-of-first-of';

About

array-handling functions that search for instances of a value and remove them

Topics

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published