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

3.0.0 • Public • Published

getByIndexes<T>(
     indexes: number[],
     array: T[]
): T[]

Returns multiple items from array, identified by their indexes.
indexes can be negative or positive.
The returned items are listed in the same order as indexes. It doesn't modify array.

Examples

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

getByIndexes([-1, 3, -3], arr);
// --> [7, 4, 5]

getByIndexes([3, -7], arr);
// --> [4, 1]

getByIndexes([-8], arr); // triggers error, since arr only has 7 items.
// Error: "The entered index is not valid.  Whether positive or negative, it exceeds 
//         the index range of the array."

getByIndexes([3, 3, 3], arr);
// --> [4, 4, 4]

getByIndexes([3, 0, 3, 0], arr);
// --> [4, 1, 4, 1]


// Behavior when passed unexpected types:

getByIndexes([0, 2], 'abc');
// --> [ 'a', 'c' ]

getByIndexes([0 ,2], {});
// Error: "The entered index is not valid.  Whether positive or negative, it exceeds 
//         the index range of the array."

getByIndexes([0, 2], true);
// Error: "The entered index is not valid.  Whether positive or negative, it exceeds 
//         the index range of the array."

getByIndexes({}, arr);
// Error: "Input must be array"

Installation

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

Loading

import { getByIndexes } from '@writetome51/array-get-by-indexes';

Package Sidebar

Install

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

Weekly Downloads

0

Version

3.0.0

License

MIT

Unpacked Size

3.88 kB

Total Files

5

Last publish

Collaborators

  • writetome51