3.0.0 • Published 3 years ago

@writetome51/array-get-by-indexes v3.0.0

Weekly downloads
-
License
MIT
Repository
github
Last release
3 years ago

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