3.0.3 • Published 3 years ago
@writetome51/array-get-and-remove-by-test v3.0.3
getAndRemoveByTest\<T, X>( test: ( value: T, index?: number, array?: T[] ) => boolean, array: T[], getValue?: ( value: T, index?: number, array?: T[] ) => X): X[]
Removes and returns items in array
that pass test
.
Includes optional callback getValue()
, which lets you customize what value to get from an element
that passes test
.
Examples
let arr = ['hello', '??', 2, 'h', 1, 5, 'h', 'mertyujkl;', 20];
getAndRemoveByTest((item) => (item[0] === 'h'), arr);
// --> ['hello', 'h', 'h']
// arr is now ['??', 2, 1, 5, 'mertyujkl;', 20];
arr = [true, 10, false, 2, 'h', 1, true];
getAndRemoveByTest(
(item) => typeof item === 'boolean',
arr,
(value, index) => ( {value, index} )
);
/**********
Returns:
[
{ value: true, index: 0 },
{ value: false, index: 2 },
{ value: true, index: 6 }
]
arr is now [10, 2, 'h', 1];
**********/
Installation
npm i @writetome51/array-get-and-remove-by-test
Loading
import {getAndRemoveByTest} from '@writetome51/array-get-and-remove-by-test';