JSPM

@writetome51/array-remove-by-indexes

4.0.0
  • ESM via JSPM
  • ES Module Entrypoint
  • Export Map
  • Keywords
  • License
  • Repository URL
  • TypeScript Types
  • README
  • Created
  • Published
  • Downloads 8
  • Score
    100M100P100Q53295F
  • License MIT

Removes items from array, identified by indexes

Package Exports

  • @writetome51/array-remove-by-indexes

This package does not declare an exports field, so the exports above have been automatically detected and optimized by JSPM instead. If any package subpath is missing, it is recommended to post an issue to the original package (@writetome51/array-remove-by-indexes) to support the "exports" field. If that is not possible, create a JSPM override to customize the exports field for this package.

Readme

removeByIndexes<T>(
     indexes: number[],
     array: T[],
     useRemovedItem?: (item: T) => void
): void

Removes items, identified by indexes, from array.
Negative indexes not allowed.
Optional callback useRemovedItem() lets you collect each removed item
for further manipulation.
The algorithm first sorts indexes in ascending order. Then the items of
array are removed in descending index order, i.e., item with index 0 would
be removed last.

Examples

let arr = [10,20,30,40,50,60];
removeByIndexes([3, 1], arr);
// arr is now [10, 30, 50, 60].


arr = ['he', 'llo', 'zz', 'gg', 'cc'];
let removed = [];

removeByIndexes([4, 0], arr, (item) => removed.push(item));
// removed is ['cc', 'he'] (remember, elements are removed in descending index order).
// arr is ['llo', 'zz', 'gg']. 


arr = [10,20,30,40,50,60];
removed = [];

removeByIndexes([1, 3, 5], arr, (item) => removed.push(item));
// removed is [60, 40, 20]
// arr is [10, 30, 50].


// It can work with duplicate indexes, though the result may not be
// what you wanted:

arr = ['he', 'llo', 'zz', 'gg', 'cc', 'aa'];
removeByIndexes([2, 3, 4, 4], arr);
// arr is ['he', 'llo']

arr = ['he', 'llo', 'zz', 'gg', 'cc', 'aa'];
removeByIndexes([1, 1, 1], arr);
// arr is ['he', 'cc', 'aa']

// Don't pass string numbers. You'll get confusing errors or unexpected results.
removeByIndexes(['1'], arr);
// Error: "the array does not have enough items to fulfill your request"

Installation

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

Loading

import {removeByIndexes} from '@writetome51/array-remove-by-indexes';