@writetome51/array-remove-by-test
TypeScript icon, indicating that this package has built-in type declarations

2.1.0 • Public • Published

removeByTest(
      test: (value, index?, array?) => boolean,
      array,
      useRemovedItem?: (
            value, index?, array?
      ) => void
): void

Removes any item in array that passes a test.
If you use the optional callback useRemovedItem(), remember that this function
removes items in descending index-order, i.e., item with index 0 is removed last.

Example

let arr = [1,2,3,4,5,6,7,8,9,10];

removeByTest((item) => (item % 2) === 0,  arr);
console.log(arr);
// [1,3,5,7,9]

  
arr = [ [10, 2, 3], [2, 3, 4], 1, 6, false, [10, 20] ];

removeByTest(
    (item) => (Array.isArray(item) && item[0] === 10),
    arr
);
console.log(arr);
// [ [2, 3, 4], 1, 6, false ]


// Collect the indexes of removed items:

arr = [10, 1, 20, 2, 30, 3, 40, 4];

let found = [];
removeByTest(
    (value) => value >= 10, 
    arr, 
    (value, index) => found.push(index)
);
console.log(found);
// [ 6, 4, 2, 0 ] 
// (items removed in descending index-order)

Installation

npm i @writetome51/array-remove-by-test

Loading

import {removeByTest} from '@writetome51/array-remove-by-test';

Package Sidebar

Install

npm i @writetome51/array-remove-by-test

Weekly Downloads

1

Version

2.1.0

License

MIT

Unpacked Size

3.81 kB

Total Files

5

Last publish

Collaborators

  • writetome51