Skip to content

Receives a test function and both removes and returns array items that pass test

License

Notifications You must be signed in to change notification settings

writetome51/array-get-and-remove-by-test

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

27 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

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';

About

Receives a test function and both removes and returns array items that pass test

Topics

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published