Extended native JavaScript Array which provides indexed lookup similar to native Map.
- Installation
- Description
- Synopsis
- Details
- API
- indexable-array
- Classes
- Class: IndexableArray <I, DK, OK, TH>
- Type parameters
- Hierarchy
- Indexable
- Properties
- Methods
- __@iterator
- __@unscopables
- concat
- copyWithin
- disableIndex
- enableIndex
- entries
- every
- fill
- filter
- find
- findIndex
- flat
- flatMap
- forEach
- get
- getAll
- getAllIndexes
- getIndex
- getMaybe
- getSure
- has
- includes
- indexOf
- join
- keys
- lastIndexOf
- map
- mapToArray
- pop
- push
- reduce
- reduceRight
- reverse
- set
- shift
- slice
- some
- sort
- sortBy
- splice
- toLocaleString
- toString
- unshift
- values
- withDefaultIndex
Static
fromStatic
throwingFrom
npm install indexable-array
indexable-array
extends native JavaScript array and provides indexed lookup features similar to native Map
by using
Proxy
for shallow change detection.
import IndexableArray, { Self } from "indexable-array";
const users = new IndexableArray({ id: 23, name: "George" }, { id: 92, name: "George" }).addIndex("name", Self);
const otherUsers = new IndexableArray({ id: 12, name: "Hans" }, { id: 18, name: "Tanja" }).addIndex("name").addSelfIndex();
Array.isArray(users); // true
users.getIndex("George"); // 1
users.get("George"); // Get first George: { id: 23, name: "George"}
users.get("George", { fromIndex: 1 }); // Get first George starting from index 1: { id: 23, name: "George"}
users.getAllIndexes("George"); // [0, 1]
// Replace George with Henry
const newUser = { id: 21, name: "Henry" };
users[0] = newUser;
users.getIndex(newUser); // 0 - It is possible to index whole object by { selfIndex: true } option.
// Add another Henry
users.splice(1, 1, { id: 34, name: "Henry" });
users.getAllIndexes("Henry"); // [0, 1];
// You may want to disable en re-enable index for heavy updates for performance reasons.
users.disableIndex(); // Disable index before heavy updates.
// ... many many many updates here
users.enableIndex(); // Index is recreated from scratch.
// Do NOT change deeply nested values in indexed fields.
// users[0].name = "DON'T DO THIS"; // WRONG: Sub fields (i.e. [0]."name") of the array is not watched, so index does not get updated.
// To change nested values use `set()`
users.set(0, "name", "OK"); // Index updated.
// or (not preferred because of expensive index creation for a small update)
users.disableIndex();
users[0].name = "Prefer set()";
users.enableIndex(); // Index is recreated from scratch.
- Written in TypeScript.
- Is a native
Array
(Array.isArray(indexableArray) === true
), so supports all array features. - 100% test coverage.
- Tracks all shallow changes via by using
Proxy
- Limited support for updating deep properties via
set()
method. - Uses
map
to index for very fast lookups. - Uses binary search for updates for faster index update.
- Disables and recreates index from scratch automatically for heavy update operations like
splice
if above threshold.. - Indexing may be disabled and re-enabled for heavy update operations manually.
- Uses binary search for
indexOf()
,lastIndexOf()
,has()
if user added self index. - Methods such as
map()
,filter()
,slice()
returnsIndexedArray
. Additionally providesmapIndexed()
method.
indexable-array › IndexableArray
Extended native array class to access array elements by fast key lookups using binary search. Used for storing objects.
import IndexableArray, { Self } from "indexable-array";
const users = new IndexableArray({ id: 23, name: "Geroge" }, { id: 96, name: "Lisa" }).addIndex("name");
Array.isArray(users); // true
users.get("George"); // { id: 23, name: "George"}
const user = { id: 21, name: "Henry" };
users[0] = user;
users.getIndex(user); // 0 - It is possible to index whole object by { selfIndex: true } option.
users.splice(1, 1, { id: 34, name: "Henry" });
users.getAllIndexes("Henry"); // [0, 1];
users[0].name = "DON'T DO THIS"; // WRONG: Sub fields (i.e. [0]."name") of the array is not watched, so index does not get updated.
users.set(0, "name", "OK"); // Index updated.
users.disableIndex();
users[0].name = "THIS IS OK NOW";
users.enableIndex(); // Index is recreated from scratch.
▪ I: any
▪ DK: keyof I
▪ OK: keyof I
▪ TH: boolean
-
Array‹I›
↳ IndexableArray
- [ n: number]: I
Extended native array class to access array elements by fast key lookups using binary search. Used for storing objects.
import IndexableArray, { Self } from "indexable-array";
const users = new IndexableArray({ id: 23, name: "Geroge" }, { id: 96, name: "Lisa" }).addIndex("name");
Array.isArray(users); // true
users.get("George"); // { id: 23, name: "George"}
const user = { id: 21, name: "Henry" };
users[0] = user;
users.getIndex(user); // 0 - It is possible to index whole object by { selfIndex: true } option.
users.splice(1, 1, { id: 34, name: "Henry" });
users.getAllIndexes("Henry"); // [0, 1];
users[0].name = "DON'T DO THIS"; // WRONG: Sub fields (i.e. [0]."name") of the array is not watched, so index does not get updated.
users.set(0, "name", "OK"); // Index updated.
users.disableIndex();
users[0].name = "THIS IS OK NOW";
users.enableIndex(); // Index is recreated from scratch.
• indexedKeys: Set‹DK | OK› = new Set()
Defined in src/index.ts:82
Set of the indexed key names. $$self
is used for the whole value.
const users = new IndexableArray({ id: 23, name: "Geroge" }, { id: 96, name: "Lisa" }).addSelfIndex().addIndex("name");
users.indexedArray; // ["$$self", "name"]
• length: number
Inherited from void
Defined in node_modules/typescript/lib/lib.es5.d.ts:1215
Gets or sets the length of the array. This is a number one higher than the highest element defined in an array.
▪ Array: ArrayConstructor
Defined in node_modules/typescript/lib/lib.es5.d.ts:1385
▸ __@iterator(): IterableIterator‹I›
Inherited from void
Defined in node_modules/typescript/lib/lib.es2015.iterable.d.ts:60
Iterator
Returns: IterableIterator‹I›
▸ __@unscopables(): object
Inherited from void
Defined in node_modules/typescript/lib/lib.es2015.symbol.wellknown.d.ts:94
Returns an object whose properties have the value 'true' when they will be absent when used in a 'with' statement.
Returns: object
▸ concat(...items
: ConcatArray‹I›[]): IndexableArray‹I, DK, OK, TH›
Overrides void
Defined in src/index.ts:651
Parameters:
Name | Type |
---|---|
...items |
ConcatArray‹I›[] |
Returns: IndexableArray‹I, DK, OK, TH›
▸ concat(...items
: I | ConcatArray‹I›[]): IndexableArray‹I, DK, OK, TH›
Overrides void
Defined in src/index.ts:652
Parameters:
Name | Type |
---|---|
...items |
I | ConcatArray‹I›[] |
Returns: IndexableArray‹I, DK, OK, TH›
▸ copyWithin(target
: number, start
: number, end?
: undefined | number): this
Inherited from void
Defined in node_modules/typescript/lib/lib.es2015.core.d.ts:64
Returns the this object after copying a section of the array identified by start and end to the same array starting at position target
Parameters:
Name | Type | Description |
---|---|---|
target |
number | If target is negative, it is treated as length+target where length is the length of the array. |
start |
number | If start is negative, it is treated as length+start. If end is negative, it is treated as length+end. |
end? |
undefined | number | If not specified, length of the this object is used as its default value. |
Returns: this
▸ disableIndex(): void
Defined in src/index.ts:836
Disables indexing of the array. It may be used to disable temporarily
- to do heavy updates for performance reasons,
- to do operations in sub fields. If indexing is not needed anymore, it is suggested to create a new native non-extended array and copy values into it for avoiding performance penalty of proxy array used in this library.
indexedArray.disableIndex();
indexedArray[0].name = "THIS IS OK NOW";
indexedArray.enableIndex(); // Index is recreated from scratch.
see
{IndexedArray#enableIndex} method.
Returns: void
▸ enableIndex(): void
Defined in src/index.ts:845
Enables indexing and recreates index from scratch.
see
{IndexedArray#disableIndex} method.
Returns: void
▸ entries(): IterableIterator‹[number, I]›
Inherited from void
Defined in node_modules/typescript/lib/lib.es2015.iterable.d.ts:65
Returns an iterable of key, value pairs for every entry in the array
Returns: IterableIterator‹[number, I]›
▸ every(callbackfn
: function, thisArg?
: any): boolean
Inherited from void
Defined in node_modules/typescript/lib/lib.es5.d.ts:1310
Determines whether all the members of an array satisfy the specified test.
Parameters:
▪ callbackfn: function
A function that accepts up to three arguments. The every method calls the callbackfn function for each element in the array until the callbackfn returns a value which is coercible to the Boolean value false, or until the end of the array.
▸ (value
: I, index
: number, array
: I[]): unknown
Parameters:
Name | Type |
---|---|
value |
I |
index |
number |
array |
I[] |
▪Optional
thisArg: any
An object to which the this keyword can refer in the callbackfn function. If thisArg is omitted, undefined is used as the this value.
Returns: boolean
▸ fill(value
: I, start?
: undefined | number, end?
: undefined | number): this
Inherited from void
Defined in node_modules/typescript/lib/lib.es2015.core.d.ts:53
Returns the this object after filling the section identified by start and end with value
Parameters:
Name | Type | Description |
---|---|---|
value |
I | value to fill array section with |
start? |
undefined | number | index to start filling the array at. If start is negative, it is treated as length+start where length is the length of the array. |
end? |
undefined | number | index to stop filling the array at. If end is negative, it is treated as length+end. |
Returns: this
▸ filter<S>(callbackfn
: function, thisArg?
: any): IndexableArray‹S, DK, OK, TH›
Overrides void
Defined in src/index.ts:471
Type parameters:
▪ S: I
Parameters:
▪ callbackfn: function
▸ (value
: I, index
: number, array
: IndexableArray‹I, DK, OK, TH›): value is S
Parameters:
Name | Type |
---|---|
value |
I |
index |
number |
array |
IndexableArray‹I, DK, OK, TH› |
▪Optional
thisArg: any
Returns: IndexableArray‹S, DK, OK, TH›
▸ filter(callbackfn
: Callback‹I, DK, OK, TH, unknown›, thisArg?
: any): IndexableArray‹I, DK, OK, TH›
Overrides void
Defined in src/index.ts:476
Parameters:
Name | Type |
---|---|
callbackfn |
Callback‹I, DK, OK, TH, unknown› |
thisArg? |
any |
Returns: IndexableArray‹I, DK, OK, TH›
▸ find<S>(predicate
: function, thisArg?
: any): S | undefined
Inherited from void
Defined in node_modules/typescript/lib/lib.es2015.core.d.ts:31
Returns the value of the first element in the array where predicate is true, and undefined otherwise.
Type parameters:
▪ S: I
Parameters:
▪ predicate: function
find calls predicate once for each element of the array, in ascending order, until it finds one where predicate returns true. If such an element is found, find immediately returns that element value. Otherwise, find returns undefined.
▸ (this
: void, value
: I, index
: number, obj
: I[]): value is S
Parameters:
Name | Type |
---|---|
this |
void |
value |
I |
index |
number |
obj |
I[] |
▪Optional
thisArg: any
If provided, it will be used as the this value for each invocation of predicate. If it is not provided, undefined is used instead.
Returns: S | undefined
▸ find(predicate
: function, thisArg?
: any): I | undefined
Inherited from void
Defined in node_modules/typescript/lib/lib.es2015.core.d.ts:32
Parameters:
▪ predicate: function
▸ (value
: I, index
: number, obj
: I[]): unknown
Parameters:
Name | Type |
---|---|
value |
I |
index |
number |
obj |
I[] |
▪Optional
thisArg: any
Returns: I | undefined
▸ findIndex(predicate
: function, thisArg?
: any): number
Inherited from void
Defined in node_modules/typescript/lib/lib.es2015.core.d.ts:43
Returns the index of the first element in the array where predicate is true, and -1 otherwise.
Parameters:
▪ predicate: function
find calls predicate once for each element of the array, in ascending order, until it finds one where predicate returns true. If such an element is found, findIndex immediately returns that element index. Otherwise, findIndex returns -1.
▸ (value
: I, index
: number, obj
: I[]): unknown
Parameters:
Name | Type |
---|---|
value |
I |
index |
number |
obj |
I[] |
▪Optional
thisArg: any
If provided, it will be used as the this value for each invocation of predicate. If it is not provided, undefined is used instead.
Returns: number
▸ flat<U>(this
: U[][][][][][][][], depth
: 7): U[]
Inherited from void
Defined in node_modules/typescript/lib/lib.es2019.array.d.ts:158
Returns a new array with all sub-array elements concatenated into it recursively up to the specified depth.
Type parameters:
▪ U
Parameters:
Name | Type | Description |
---|---|---|
this |
U[][][][][][][][] | - |
depth |
7 | The maximum recursion depth |
Returns: U[]
▸ flat<U>(this
: U[][][][][][][], depth
: 6): U[]
Inherited from void
Defined in node_modules/typescript/lib/lib.es2019.array.d.ts:166
Returns a new array with all sub-array elements concatenated into it recursively up to the specified depth.
Type parameters:
▪ U
Parameters:
Name | Type | Description |
---|---|---|
this |
U[][][][][][][] | - |
depth |
6 | The maximum recursion depth |
Returns: U[]
▸ flat<U>(this
: U[][][][][][], depth
: 5): U[]
Inherited from void
Defined in node_modules/typescript/lib/lib.es2019.array.d.ts:174
Returns a new array with all sub-array elements concatenated into it recursively up to the specified depth.
Type parameters:
▪ U
Parameters:
Name | Type | Description |
---|---|---|
this |
U[][][][][][] | - |
depth |
5 | The maximum recursion depth |
Returns: U[]
▸ flat<U>(this
: U[][][][][], depth
: 4): U[]
Inherited from void
Defined in node_modules/typescript/lib/lib.es2019.array.d.ts:182
Returns a new array with all sub-array elements concatenated into it recursively up to the specified depth.
Type parameters:
▪ U
Parameters:
Name | Type | Description |
---|---|---|
this |
U[][][][][] | - |
depth |
4 | The maximum recursion depth |
Returns: U[]
▸ flat<U>(this
: U[][][][], depth
: 3): U[]
Inherited from void
Defined in node_modules/typescript/lib/lib.es2019.array.d.ts:190
Returns a new array with all sub-array elements concatenated into it recursively up to the specified depth.
Type parameters:
▪ U
Parameters:
Name | Type | Description |
---|---|---|
this |
U[][][][] | - |
depth |
3 | The maximum recursion depth |
Returns: U[]
▸ flat<U>(this
: U[][][], depth
: 2): U[]
Inherited from void
Defined in node_modules/typescript/lib/lib.es2019.array.d.ts:198
Returns a new array with all sub-array elements concatenated into it recursively up to the specified depth.
Type parameters:
▪ U
Parameters:
Name | Type | Description |
---|---|---|
this |
U[][][] | - |
depth |
2 | The maximum recursion depth |
Returns: U[]
▸ flat<U>(this
: U[][], depth?
: undefined | 1): U[]
Inherited from void
Defined in node_modules/typescript/lib/lib.es2019.array.d.ts:206
Returns a new array with all sub-array elements concatenated into it recursively up to the specified depth.
Type parameters:
▪ U
Parameters:
Name | Type | Description |
---|---|---|
this |
U[][] | - |
depth? |
undefined | 1 | The maximum recursion depth |
Returns: U[]
▸ flat<U>(this
: U[], depth
: 0): U[]
Inherited from void
Defined in node_modules/typescript/lib/lib.es2019.array.d.ts:214
Returns a new array with all sub-array elements concatenated into it recursively up to the specified depth.
Type parameters:
▪ U
Parameters:
Name | Type | Description |
---|---|---|
this |
U[] | - |
depth |
0 | The maximum recursion depth |
Returns: U[]
▸ flat<U>(depth?
: undefined | number): any[]
Inherited from void
Defined in node_modules/typescript/lib/lib.es2019.array.d.ts:222
Returns a new array with all sub-array elements concatenated into it recursively up to the specified depth. If no depth is provided, flat method defaults to the depth of 1.
Type parameters:
▪ U
Parameters:
Name | Type | Description |
---|---|---|
depth? |
undefined | number | The maximum recursion depth |
Returns: any[]
▸ flatMap<U, DK2, OK2, This>(callbackFn
: CallbackThis‹I, DK, OK, TH, U | keyof U[], This›, defaultKey?
: DK2, ...indexKeys
: OK2[]): IndexableArray‹U, DK2, Exclude‹OK2, DK2›, TH›
Overrides void
Defined in src/index.ts:558
Calls a defined callback function on each element of an indexable array. Then, flattens the result into a new indexable array. This is identical to a map followed by flat with depth 1.
Type parameters:
▪ U: Pick‹I, DK | OK›
▪ DK2: keyof U
▪ OK2: keyof U
▪ This: undefined | object
Parameters:
Name | Type | Description |
---|---|---|
callbackFn |
CallbackThis‹I, DK, OK, TH, U | keyof U[], This› | is a function that accepts up to three arguments. The flatMap method calls the callback function one time for each element in the array. |
defaultKey? |
DK2 | - |
...indexKeys |
OK2[] | - |
Returns: IndexableArray‹U, DK2, Exclude‹OK2, DK2›, TH›
a new IndexableArray
of dept 1.
▸ flatMap<U, DK2, OK2, This>(callbackFn
: CallbackThis‹I, DK, OK, TH, U | keyof U[], This›, thisArg
: object, defaultKey?
: DK2, ...indexKeys
: OK2[]): IndexableArray‹U, DK2, Exclude‹OK2, DK2›, TH›
Overrides void
Defined in src/index.ts:569
Calls a defined callback function on each element of an indexable array. Then, flattens the result into a new indexable array. This is identical to a map followed by flat with depth 1.
Type parameters:
▪ U: Pick‹I, DK | OK›
▪ DK2: keyof U
▪ OK2: keyof U
▪ This: undefined | object
Parameters:
Name | Type | Description |
---|---|---|
callbackFn |
CallbackThis‹I, DK, OK, TH, U | keyof U[], This› | is a function that accepts up to three arguments. The flatMap method calls the callback function one time for each element in the array. |
thisArg |
object | - |
defaultKey? |
DK2 | - |
...indexKeys |
OK2[] | - |
Returns: IndexableArray‹U, DK2, Exclude‹OK2, DK2›, TH›
a new IndexableArray
of dept 1.
▸ flatMap<U, DK2, OK2, This>(callbackFn
: CallbackThis‹I, DK, OK, TH, U | keyof U[], This›, defaultKey
: DK2, ...indexKeys
: OK2[]): IndexableArray‹U, DK2, Exclude‹OK2, DK2›, TH›
Overrides void
Defined in src/index.ts:581
Calls a defined callback function on each element of an indexable array. Then, flattens the result into a new indexable array. This is identical to a map followed by flat with depth 1.
Type parameters:
▪ U: any
▪ DK2: keyof U
▪ OK2: keyof U
▪ This: undefined | object
Parameters:
Name | Type | Description |
---|---|---|
callbackFn |
CallbackThis‹I, DK, OK, TH, U | keyof U[], This› | is a function that accepts up to three arguments. The flatMap method calls the callback function one time for each element in the array. |
defaultKey |
DK2 | - |
...indexKeys |
OK2[] | - |
Returns: IndexableArray‹U, DK2, Exclude‹OK2, DK2›, TH›
a new IndexableArray
of dept 1.
▸ flatMap<U, DK2, OK2, This>(callbackFn
: CallbackThis‹I, DK, OK, TH, U | keyof U[], This›, thisArg
: object, defaultKey
: DK2, ...indexKeys
: OK2[]): IndexableArray‹U, DK2, Exclude‹OK2, DK2›, TH›
Overrides void
Defined in src/index.ts:587
Calls a defined callback function on each element of an indexable array. Then, flattens the result into a new indexable array. This is identical to a map followed by flat with depth 1.
Type parameters:
▪ U: any
▪ DK2: keyof U
▪ OK2: keyof U
▪ This: undefined | object
Parameters:
Name | Type | Description |
---|---|---|
callbackFn |
CallbackThis‹I, DK, OK, TH, U | keyof U[], This› | is a function that accepts up to three arguments. The flatMap method calls the callback function one time for each element in the array. |
thisArg |
object | - |
defaultKey |
DK2 | - |
...indexKeys |
OK2[] | - |
Returns: IndexableArray‹U, DK2, Exclude‹OK2, DK2›, TH›
a new IndexableArray
of dept 1.
▸ flatMap<U, This>(callbackFn
: CallbackThis‹I, DK, OK, TH, U | keyof U[], This›, thisArg?
: This, ...rest
: any[]): IndexableArray‹U, AvailableDefaultIndex‹U, DK, OK›, AvailableIndex‹U, DK, OK›, TH›
Overrides void
Defined in src/index.ts:594
Calls a defined callback function on each element of an indexable array. Then, flattens the result into a new indexable array. This is identical to a map followed by flat with depth 1.
Type parameters:
▪ U: any
▪ This: undefined | object
Parameters:
Name | Type | Description |
---|---|---|
callbackFn |
CallbackThis‹I, DK, OK, TH, U | keyof U[], This› | is a function that accepts up to three arguments. The flatMap method calls the callback function one time for each element in the array. |
thisArg? |
This | - |
...rest |
any[] | - |
Returns: IndexableArray‹U, AvailableDefaultIndex‹U, DK, OK›, AvailableIndex‹U, DK, OK›, TH›
a new IndexableArray
of dept 1.
▸ forEach(callbackfn
: function, thisArg?
: any): void
Inherited from void
Defined in node_modules/typescript/lib/lib.es5.d.ts:1325
Performs the specified action for each element in an array.
Parameters:
▪ callbackfn: function
A function that accepts up to three arguments. forEach calls the callbackfn function one time for each element in the array.
▸ (value
: I, index
: number, array
: I[]): void
Parameters:
Name | Type |
---|---|
value |
I |
index |
number |
array |
I[] |
▪Optional
thisArg: any
An object to which the this keyword can refer in the callbackfn function. If thisArg is omitted, undefined is used as the this value.
Returns: void
▸ get<K, TH2>(value
: I[K], __namedParameters
: object): TH2 extends true ? I : I | undefined
Defined in src/index.ts:722
Returns the first item at which a given indexed value can be found in the array. According to construction option or throwUnknown
option,
returns undefined
or throws exception if value cannot be found.
Type parameters:
▪ K: DK | OK
▪ TH2: boolean | undefined
Parameters:
Name | Type | Default | Description |
---|---|---|---|
value |
I[K] | - | is indexed value to search for. |
__namedParameters |
object | {} | - |
Returns: TH2 extends true ? I : I | undefined
the first item with given indexed value in the array; undefined
if not found.
▸ getAll<K>(value
: I[K], __namedParameters
: object): I[]
Defined in src/index.ts:778
Returns all items at which a given indexed value can be found in the array, or empty array if it is not present.
Type parameters:
▪ K: DK | OK
Parameters:
Name | Type | Default | Description |
---|---|---|---|
value |
I[K] | - | is indexed value to search for. |
__namedParameters |
object | {} | - |
Returns: I[]
all items with given indexed value in the array; Empty array if not found.
▸ getAllIndexes<K>(value
: I[K], __namedParameters
: object): number[]
Defined in src/index.ts:706
Returns all indexes at which a given indexed value can be found in the array, or empty array if it is not present.
Type parameters:
▪ K: OK | DK
Parameters:
Name | Type | Default | Description |
---|---|---|---|
value |
I[K] | - | indexed value to search for. |
__namedParameters |
object | {} | - |
Returns: number[]
all indexes of the element in the array; Empty array if not found.
▸ getIndex<K>(value
: I[K], __namedParameters
: object): number
Defined in src/index.ts:683
Returns the first index at which a given indexed value can be found in the array, or -1 if it is not present.
Type parameters:
▪ K: DK | OK
Parameters:
Name | Type | Default | Description |
---|---|---|---|
value |
I[K] | - | indexed value to search for. |
__namedParameters |
object | {} | - |
Returns: number
the first index of the element in the array; -1 if not found.
▸ getMaybe<K>(value
: I[K], __namedParameters
: object): I | undefined
Defined in src/index.ts:763
Returns the first item at which a given indexed value can be found in the array. Returns undefined
if value cannot be found.
Type parameters:
▪ K: DK | OK
Parameters:
Name | Type | Default | Description |
---|---|---|---|
value |
I[K] | - | is indexed value to search for. |
__namedParameters |
object | {} | - |
Returns: I | undefined
is the first item with given indexed value in the array; undefined
if not found.
▸ getSure<K>(value
: I[K], __namedParameters
: object): I
Defined in src/index.ts:750
Returns the first item at which a given indexed value can be found in the array, or throws exception if it is not present.
Type parameters:
▪ K: DK | OK
Parameters:
Name | Type | Default | Description |
---|---|---|---|
value |
I[K] | - | is indexed value to search for. |
__namedParameters |
object | {} | - |
Returns: I
the first item with given indexed value in the array; undefined
if not found.
▸ has<K>(value
: I[K], __namedParameters
: object): boolean
Defined in src/index.ts:793
Determines whether an array includes a certain indexed value among its entries' keys, returning true or false as appropriate.
Type parameters:
▪ K: DK | OK
Parameters:
Name | Type | Default | Description |
---|---|---|---|
value |
I[K] | - | is indexed value to search for. |
__namedParameters |
object | {} | - |
Returns: boolean
true if indexed value is found among array's entries' keys.
▸ includes(searchElement
: I, fromIndex?
: undefined | number): boolean
Inherited from void
Defined in node_modules/typescript/lib/lib.es2016.array.include.d.ts:27
Determines whether an array includes a certain element, returning true or false as appropriate.
Parameters:
Name | Type | Description |
---|---|---|
searchElement |
I | The element to search for. |
fromIndex? |
undefined | number | The position in this array at which to begin searching for searchElement. |
Returns: boolean
▸ indexOf(searchElement
: I, fromIndex?
: undefined | number): number
Inherited from void
Defined in node_modules/typescript/lib/lib.es5.d.ts:1295
Returns the index of the first occurrence of a value in an array.
Parameters:
Name | Type | Description |
---|---|---|
searchElement |
I | The value to locate in the array. |
fromIndex? |
undefined | number | The array index at which to begin the search. If fromIndex is omitted, the search starts at index 0. |
Returns: number
▸ join(separator?
: undefined | string): string
Inherited from void
Defined in node_modules/typescript/lib/lib.es5.d.ts:1247
Adds all the elements of an array separated by the specified separator string.
Parameters:
Name | Type | Description |
---|---|---|
separator? |
undefined | string | A string used to separate one element of an array from the next in the resulting String. If omitted, the array elements are separated with a comma. |
Returns: string
▸ keys(): IterableIterator‹number›
Inherited from void
Defined in node_modules/typescript/lib/lib.es2015.iterable.d.ts:70
Returns an iterable of keys in the array
Returns: IterableIterator‹number›
▸ lastIndexOf(searchElement
: I, fromIndex?
: undefined | number): number
Inherited from void
Defined in node_modules/typescript/lib/lib.es5.d.ts:1301
Returns the index of the last occurrence of a specified value in an array.
Parameters:
Name | Type | Description |
---|---|---|
searchElement |
I | The value to locate in the array. |
fromIndex? |
undefined | number | The array index at which to begin the search. If fromIndex is omitted, the search starts at the last index in the array. |
Returns: number
▸ map<U, DK2, OK2>(callbackFn
: Callback‹I, DK, OK, TH, U›, defaultKey?
: DK2, ...indexKeys
: OK2[]): IndexableArray‹U, DK2, Exclude‹OK2, DK2›, TH›
Overrides void
Defined in src/index.ts:486
Creates a new IndexableArray
with the results of calling a provided function on every element in the calling array.
Returned IndexedArray
does not have any indexes, because callback function may return different kind of elements from source array.
To have same indexes as source IndexedArray
, use mapWithIndex()
instead.
const usersWithName = new IndexableArray({ id: 23, name: "Geroge" }, { id: 96, name: "Lisa" }).addIndex("name");
const usersWithNick = usersWithName.map(user => ({ id: user.id, nick: name.substring(0, 2) })).addIndex("nick"); // Has only "nick" index.
Type parameters:
▪ U: Pick‹I, DK | OK›
▪ DK2: keyof U
▪ OK2: keyof U
Parameters:
Name | Type | Description |
---|---|---|
callbackFn |
Callback‹I, DK, OK, TH, U› | is function that produces an element of the new Array, taking three arguments: value , index and indexableArray . |
defaultKey? |
DK2 | - |
...indexKeys |
OK2[] | - |
Returns: IndexableArray‹U, DK2, Exclude‹OK2, DK2›, TH›
a new IndexableArray
with each element being the result of the callback function.
▸ map<U, DK2, OK2>(callbackFn
: Callback‹I, DK, OK, TH, U›, thisArg
: object, defaultKey?
: DK2, ...indexKeys
: OK2[]): IndexableArray‹U, DK2, Exclude‹OK2, DK2›, TH›
Overrides void
Defined in src/index.ts:492
Creates a new IndexableArray
with the results of calling a provided function on every element in the calling array.
Returned IndexedArray
does not have any indexes, because callback function may return different kind of elements from source array.
To have same indexes as source IndexedArray
, use mapWithIndex()
instead.
const usersWithName = new IndexableArray({ id: 23, name: "Geroge" }, { id: 96, name: "Lisa" }).addIndex("name");
const usersWithNick = usersWithName.map(user => ({ id: user.id, nick: name.substring(0, 2) })).addIndex("nick"); // Has only "nick" index.
Type parameters:
▪ U: Pick‹I, DK | OK›
▪ DK2: keyof U
▪ OK2: keyof U
Parameters:
Name | Type | Description |
---|---|---|
callbackFn |
Callback‹I, DK, OK, TH, U› | is function that produces an element of the new Array, taking three arguments: value , index and indexableArray . |
thisArg |
object | - |
defaultKey? |
DK2 | - |
...indexKeys |
OK2[] | - |
Returns: IndexableArray‹U, DK2, Exclude‹OK2, DK2›, TH›
a new IndexableArray
with each element being the result of the callback function.
▸ map<U, DK2, OK2>(callbackFn
: Callback‹I, DK, OK, TH, U›, defaultKey
: DK2, ...indexKeys
: OK2[]): IndexableArray‹U, DK2, Exclude‹OK2, DK2›, TH›
Overrides void
Defined in src/index.ts:499
Creates a new IndexableArray
with the results of calling a provided function on every element in the calling array.
Returned IndexedArray
does not have any indexes, because callback function may return different kind of elements from source array.
To have same indexes as source IndexedArray
, use mapWithIndex()
instead.
const usersWithName = new IndexableArray({ id: 23, name: "Geroge" }, { id: 96, name: "Lisa" }).addIndex("name");
const usersWithNick = usersWithName.map(user => ({ id: user.id, nick: name.substring(0, 2) })).addIndex("nick"); // Has only "nick" index.
Type parameters:
▪ U: any
▪ DK2: keyof U
▪ OK2: keyof U
Parameters:
Name | Type | Description |
---|---|---|
callbackFn |
Callback‹I, DK, OK, TH, U› | is function that produces an element of the new Array, taking three arguments: value , index and indexableArray . |
defaultKey |
DK2 | - |
...indexKeys |
OK2[] | - |
Returns: IndexableArray‹U, DK2, Exclude‹OK2, DK2›, TH›
a new IndexableArray
with each element being the result of the callback function.
▸ map<U, DK2, OK2>(callbackFn
: Callback‹I, DK, OK, TH, U›, thisArg
: object, defaultKey
: DK2, ...indexKeys
: OK2[]): IndexableArray‹U, DK2, Exclude‹OK2, DK2›, TH›
Overrides void
Defined in src/index.ts:505
Creates a new IndexableArray
with the results of calling a provided function on every element in the calling array.
Returned IndexedArray
does not have any indexes, because callback function may return different kind of elements from source array.
To have same indexes as source IndexedArray
, use mapWithIndex()
instead.
const usersWithName = new IndexableArray({ id: 23, name: "Geroge" }, { id: 96, name: "Lisa" }).addIndex("name");
const usersWithNick = usersWithName.map(user => ({ id: user.id, nick: name.substring(0, 2) })).addIndex("nick"); // Has only "nick" index.
Type parameters:
▪ U: any
▪ DK2: keyof U
▪ OK2: keyof U
Parameters:
Name | Type | Description |
---|---|---|
callbackFn |
Callback‹I, DK, OK, TH, U› | is function that produces an element of the new Array, taking three arguments: value , index and indexableArray . |
thisArg |
object | - |
defaultKey |
DK2 | - |
...indexKeys |
OK2[] | - |
Returns: IndexableArray‹U, DK2, Exclude‹OK2, DK2›, TH›
a new IndexableArray
with each element being the result of the callback function.
▸ map<U>(callbackFn
: Callback‹I, DK, OK, TH, U›, thisArg?
: undefined | object): IndexableArray‹U, AvailableDefaultIndex‹U, DK, OK›, AvailableIndex‹U, DK, OK›, TH›
Overrides void
Defined in src/index.ts:512
Creates a new IndexableArray
with the results of calling a provided function on every element in the calling array.
Returned IndexedArray
does not have any indexes, because callback function may return different kind of elements from source array.
To have same indexes as source IndexedArray
, use mapWithIndex()
instead.
const usersWithName = new IndexableArray({ id: 23, name: "Geroge" }, { id: 96, name: "Lisa" }).addIndex("name");
const usersWithNick = usersWithName.map(user => ({ id: user.id, nick: name.substring(0, 2) })).addIndex("nick"); // Has only "nick" index.
Type parameters:
▪ U: any
Parameters:
Name | Type | Description |
---|---|---|
callbackFn |
Callback‹I, DK, OK, TH, U› | is function that produces an element of the new Array, taking three arguments: value , index and indexableArray . |
thisArg? |
undefined | object | - |
Returns: IndexableArray‹U, AvailableDefaultIndex‹U, DK, OK›, AvailableIndex‹U, DK, OK›, TH›
a new IndexableArray
with each element being the result of the callback function.
▸ mapToArray<U>(callbackfn
: Callback‹I, DK, OK, TH, U | keyof U[]›, thisArg?
: any): U[]
Defined in src/index.ts:643
Creates a new base Array (not IndexableArray) with the results of calling a provided function on every element in the calling array.
const usersWithName = new IndexableArray({ id: 23, name: "Geroge" }, { id: 96, name: "Lisa" }).addIndex("name");
const baseArray = usersWithName.mapToArray(user => ({ id: user.id, nick: name.substring(0, 2) })); // Normal base array.
see
{@link IndexableArray#map} to get an IndexableArray
.
Type parameters:
▪ U
Parameters:
Name | Type | Description |
---|---|---|
callbackfn |
Callback‹I, DK, OK, TH, U | keyof U[]› | is function that produces an element of the new Array, taking three arguments: value , index and indexableArray . |
thisArg? |
any | is value to use as this when executing callback. |
Returns: U[]
a new Array
with each element being the result of the callback function.
▸ pop(): I | undefined
Inherited from void
Defined in node_modules/typescript/lib/lib.es5.d.ts:1227
Removes the last element from an array and returns it.
Returns: I | undefined
▸ push(...items
: I[]): number
Overrides void
Defined in src/index.ts:427
Parameters:
Name | Type |
---|---|
...items |
I[] |
Returns: number
▸ reduce(callbackfn
: function): I
Inherited from void
Defined in node_modules/typescript/lib/lib.es5.d.ts:1349
Calls the specified callback function for all the elements in an array. The return value of the callback function is the accumulated result, and is provided as an argument in the next call to the callback function.
Parameters:
▪ callbackfn: function
A function that accepts up to four arguments. The reduce method calls the callbackfn function one time for each element in the array.
▸ (previousValue
: I, currentValue
: I, currentIndex
: number, array
: I[]): I
Parameters:
Name | Type |
---|---|
previousValue |
I |
currentValue |
I |
currentIndex |
number |
array |
I[] |
Returns: I
▸ reduce(callbackfn
: function, initialValue
: I): I
Inherited from void
Defined in node_modules/typescript/lib/lib.es5.d.ts:1350
Parameters:
▪ callbackfn: function
▸ (previousValue
: I, currentValue
: I, currentIndex
: number, array
: I[]): I
Parameters:
Name | Type |
---|---|
previousValue |
I |
currentValue |
I |
currentIndex |
number |
array |
I[] |
▪ initialValue: I
Returns: I
▸ reduce<U>(callbackfn
: function, initialValue
: U): U
Inherited from void
Defined in node_modules/typescript/lib/lib.es5.d.ts:1356
Calls the specified callback function for all the elements in an array. The return value of the callback function is the accumulated result, and is provided as an argument in the next call to the callback function.
Type parameters:
▪ U
Parameters:
▪ callbackfn: function
A function that accepts up to four arguments. The reduce method calls the callbackfn function one time for each element in the array.
▸ (previousValue
: U, currentValue
: I, currentIndex
: number, array
: I[]): U
Parameters:
Name | Type |
---|---|
previousValue |
U |
currentValue |
I |
currentIndex |
number |
array |
I[] |
▪ initialValue: U
If initialValue is specified, it is used as the initial value to start the accumulation. The first call to the callbackfn function provides this value as an argument instead of an array value.
Returns: U
▸ reduceRight(callbackfn
: function): I
Inherited from void
Defined in node_modules/typescript/lib/lib.es5.d.ts:1362
Calls the specified callback function for all the elements in an array, in descending order. The return value of the callback function is the accumulated result, and is provided as an argument in the next call to the callback function.
Parameters:
▪ callbackfn: function
A function that accepts up to four arguments. The reduceRight method calls the callbackfn function one time for each element in the array.
▸ (previousValue
: I, currentValue
: I, currentIndex
: number, array
: I[]): I
Parameters:
Name | Type |
---|---|
previousValue |
I |
currentValue |
I |
currentIndex |
number |
array |
I[] |
Returns: I
▸ reduceRight(callbackfn
: function, initialValue
: I): I
Inherited from void
Defined in node_modules/typescript/lib/lib.es5.d.ts:1363
Parameters:
▪ callbackfn: function
▸ (previousValue
: I, currentValue
: I, currentIndex
: number, array
: I[]): I
Parameters:
Name | Type |
---|---|
previousValue |
I |
currentValue |
I |
currentIndex |
number |
array |
I[] |
▪ initialValue: I
Returns: I
▸ reduceRight<U>(callbackfn
: function, initialValue
: U): U
Inherited from void
Defined in node_modules/typescript/lib/lib.es5.d.ts:1369
Calls the specified callback function for all the elements in an array, in descending order. The return value of the callback function is the accumulated result, and is provided as an argument in the next call to the callback function.
Type parameters:
▪ U
Parameters:
▪ callbackfn: function
A function that accepts up to four arguments. The reduceRight method calls the callbackfn function one time for each element in the array.
▸ (previousValue
: U, currentValue
: I, currentIndex
: number, array
: I[]): U
Parameters:
Name | Type |
---|---|
previousValue |
U |
currentValue |
I |
currentIndex |
number |
array |
I[] |
▪ initialValue: U
If initialValue is specified, it is used as the initial value to start the accumulation. The first call to the callbackfn function provides this value as an argument instead of an array value.
Returns: U
▸ reverse(): I[]
Inherited from void
Defined in node_modules/typescript/lib/lib.es5.d.ts:1251
Reverses the elements in an Array.
Returns: I[]
▸ set(position
: number, path
: string, value
: any): void
Defined in src/index.ts:810
Sets value at path of the object, which is one of the entires of array. To update fields of the objects, this method should be used. Otherwise index cannot be updated, because sub fileds are not tracked for chage detection.
indexedArray[0].name = "DON'T DO THIS"; // WRONG: Sub fields (i.e. [0]."name") of the array is not watched, so index does not get updated.
indexedArray.set(0, "name", "OK"); // Index updated.
Parameters:
Name | Type | Description |
---|---|---|
position |
number | is index of the item to be changed. |
path |
string | is item's path where value to be changed at. |
value |
any | is new value to be assigned. |
Returns: void
▸ shift(): I | undefined
Inherited from void
Defined in node_modules/typescript/lib/lib.es5.d.ts:1255
Removes the first element from an array and returns it.
Returns: I | undefined
▸ slice(start?
: undefined | number, end?
: undefined | number): IndexableArray‹I, DK, OK, TH›
Overrides void
Defined in src/index.ts:647
Parameters:
Name | Type |
---|---|
start? |
undefined | number |
end? |
undefined | number |
Returns: IndexableArray‹I, DK, OK, TH›
▸ some(callbackfn
: function, thisArg?
: any): boolean
Inherited from void
Defined in node_modules/typescript/lib/lib.es5.d.ts:1319
Determines whether the specified callback function returns true for any element of an array.
Parameters:
▪ callbackfn: function
A function that accepts up to three arguments. The some method calls the callbackfn function for each element in the array until the callbackfn returns a value which is coercible to the Boolean value true, or until the end of the array.
▸ (value
: I, index
: number, array
: I[]): unknown
Parameters:
Name | Type |
---|---|
value |
I |
index |
number |
array |
I[] |
▪Optional
thisArg: any
An object to which the this keyword can refer in the callbackfn function. If thisArg is omitted, undefined is used as the this value.
Returns: boolean
▸ sort(compareFn?
: undefined | function): this
Overrides void
Defined in src/index.ts:446
Parameters:
Name | Type |
---|---|
compareFn? |
undefined | function |
Returns: this
▸ sortBy(key
: DK | OK): this
Defined in src/index.ts:459
Sorts the elements of an array by given key in place and returns the sorted array.
Parameters:
Name | Type | Default | Description |
---|---|---|---|
key |
DK | OK | this.defaultKey | is the key to sort array by. |
Returns: this
this instance.
▸ splice(start
: number, deleteCount
: number, ...items
: I[]): I[]
Overrides void
Defined in src/index.ts:434
Parameters:
Name | Type | Default |
---|---|---|
start |
number | - |
deleteCount |
number | this.length |
...items |
I[] | - |
Returns: I[]
▸ toLocaleString(): string
Inherited from void
Defined in node_modules/typescript/lib/lib.es5.d.ts:1223
Returns a string representation of an array. The elements are converted to string using their toLocalString methods.
Returns: string
▸ toString(): string
Inherited from void
Defined in node_modules/typescript/lib/lib.es5.d.ts:1219
Returns a string representation of an array.
Returns: string
▸ unshift(...items
: I[]): number
Inherited from void
Defined in node_modules/typescript/lib/lib.es5.d.ts:1289
Inserts new elements at the start of an array.
Parameters:
Name | Type | Description |
---|---|---|
...items |
I[] | Elements to insert at the start of the Array. |
Returns: number
▸ values(): IterableIterator‹I›
Inherited from void
Defined in node_modules/typescript/lib/lib.es2015.iterable.d.ts:75
Returns an iterable of values in the array
Returns: IterableIterator‹I›
▸ withDefaultIndex<K>(key
: K): IndexableArray‹I, K, OK, TH›
Defined in src/index.ts:669
Sets default index key to be used with lookup functions. Returns same instance.
const input = [
{ id: 23, name: "Geroge" },
{ id: 96, name: "Lisa" },
];
let users = new IndexableArray(...input).addIndex("name", "id"); // "name" is default index
users = users.withDefaultIndex("id"); // "id" is default index. Assignment is used for TypeScript to assign right type to variable.
Type parameters:
▪ K: OK
Parameters:
Name | Type | Description |
---|---|---|
key |
K | is key to be used as default index with lookup functions. |
Returns: IndexableArray‹I, K, OK, TH›
this object.
▸ from<I2, DK2, DK3, OK2, OK3, TH2>(indexableArray
: IndexableArray‹I2, DK2, OK2, TH2›, defaultKey?
: DK3, ...indexKeys
: OK3[]): IndexableArray‹I2, DK3, Exclude‹OK3, DK3›, TH2›
Defined in src/index.ts:100
Creates a new, shallow-copied IndexableArray
instance from an array-like or iterable object. If source is also IndexableArray
,
returned IndexableArray
will have same indexed keys.
Type parameters:
▪ I2: any
▪ DK2: keyof I2
▪ DK3: keyof I2
▪ OK2: keyof I2
▪ OK3: keyof I2
▪ TH2: boolean
Parameters:
Name | Type | Description |
---|---|---|
indexableArray |
IndexableArray‹I2, DK2, OK2, TH2› | - |
defaultKey? |
DK3 | is default key to be used with get() method if no key is provided. |
...indexKeys |
OK3[] | are keys to be indexed. |
Returns: IndexableArray‹I2, DK3, Exclude‹OK3, DK3›, TH2›
a new IndexableArray
instance.
▸ from<I2, DK2, OK2>(arrayLike
: Iterable‹I2› | ArrayLike‹I2›, defaultKey
: DK2, ...indexKeys
: OK2[]): IndexableArray‹I2, DK2, Exclude‹OK2, DK2›, false›
Defined in src/index.ts:113
Creates a new, shallow-copied IndexableArray
instance from an array-like or iterable object. If source is also IndexableArray
,
returned IndexableArray
will have same indexed keys.
Type parameters:
▪ I2
▪ DK2: keyof I2
▪ OK2: keyof I2
Parameters:
Name | Type | Description |
---|---|---|
arrayLike |
Iterable‹I2› | ArrayLike‹I2› | is an array-like or iterable object to convert to an array. |
defaultKey |
DK2 | is default key to be used with get() method if no key is provided. |
...indexKeys |
OK2[] | are keys to be indexed. |
Returns: IndexableArray‹I2, DK2, Exclude‹OK2, DK2›, false›
a new IndexableArray
instance.
▸ throwingFrom<I2, DK2, DK3, OK2, OK3, TH2>(indexableArray
: IndexableArray‹I2, DK2, OK2, TH2›, defaultKey?
: DK3, ...indexKeys
: OK3[]): IndexableArray‹I2, DK3, Exclude‹OK3, DK3›, true›
Defined in src/index.ts:159
Creates a new, shallow-copied IndexableArray
instance from an array-like or iterable object. If source is also IndexableArray
,
returned IndexableArray
will have same indexed keys. Returned instance throws exception if get()
methods cannot find given value.
Type parameters:
▪ I2: any
▪ DK2: keyof I2
▪ DK3: keyof I2
▪ OK2: keyof I2
▪ OK3: keyof I2
▪ TH2: boolean
Parameters:
Name | Type | Description |
---|---|---|
indexableArray |
IndexableArray‹I2, DK2, OK2, TH2› | - |
defaultKey? |
DK3 | is default key to be used with get() method if no key is provided. |
...indexKeys |
OK3[] | are keys to be indexed. |
Returns: IndexableArray‹I2, DK3, Exclude‹OK3, DK3›, true›
a new IndexableArray
instance.
▸ throwingFrom<I2, DK2, OK2>(arrayLike
: Iterable‹I2› | ArrayLike‹I2›, defaultKey
: DK2, ...indexKeys
: OK2[]): IndexableArray‹I2, DK2, Exclude‹OK2, DK2›, true›
Defined in src/index.ts:172
Creates a new, shallow-copied IndexableArray
instance from an array-like or iterable object. If source is also IndexableArray
,
returned IndexableArray
will have same indexed keys. Returned instance throws exception if get()
methods cannot find given value.
Type parameters:
▪ I2
▪ DK2: keyof I2
▪ OK2: keyof I2
Parameters:
Name | Type | Description |
---|---|---|
arrayLike |
Iterable‹I2› | ArrayLike‹I2› | is an array-like or iterable object to convert to an array. |
defaultKey |
DK2 | is default key to be used with get() method if no key is provided. |
...indexKeys |
OK2[] | are keys to be indexed. |
Returns: IndexableArray‹I2, DK2, Exclude‹OK2, DK2›, true›
a new IndexableArray
instance.