Module: rgjs/array

RGJS6 Array module.

Methods


<static> contains()

Check if an array contains a value.
Alias of includes().

See:
  • rgjs/array/includes

<static> copy(array)

Copy an array.
Alias of rgjs.obj.copy().

Parameters:
Name Type Description
array array

The array to be copied.

See:
  • rgjs/obj/copy
Returns:

A copy of the array.

Type
array
Example
rgjs.array.copy([1, 2, 3]); // [1, 2, 3]

<static> findIndex(array, path, value)

Find the index of an object's value.

Parameters:
Name Type Description
array array

An array containing objects.

path string

The path of the object's value. See obj.getVal().

value *

The value to compare array[index].path with.

Returns:
Type
number
Example
rgjs.array.findIndex([{value: 1}, {value: 2}], "value", 1) // 0
rgjs.array.findIndex([{value: 1}, {value: 2}], "value", 3) // -1

<static> forEachAsync(array, each_fn, finished_cb, opts)

Iterate array in chunks.
Based on https://stackoverflow.com/a/10344560/781094.

Parameters:
Name Type Description
array array

The array to iterate.

each_fn function

Function that is called for each array element. Arguments: array[index], index, array.

finished_cb function

Optional callback for when iteration has completed.

opts object

Optional options.

Properties
Name Type Description
maxTimePerChunk number

Optional max time each chunk is iterated before releasing the thread. Default: 16.

bind object

Optional context for the fn parameter.


<static> fromList(list)

Convert a list object to an array.
Useful for converting array-like objects like arguments, NodeList, etc to an array.

Parameters:
Name Type Description
list list

A list object.

Returns:

An array.

Type
array
Example
// Returns [1, 2, 3]
function f() {
  return rgjs.array.fromList(arguments);
}
f(1, 2, 3);

<static> fromObject(obj [, keyKey])

Convert an object to an array.

Parameters:
Name Type Argument Description
obj object

An object.

keyKey string <optional>

Optional. If object contains objects, obj[k][keyKey] will be set to 'k'.

Returns:

An array.

Type
array
Examples
// Simply convert object to array
// Returns [{name: 'Adam'}, {name: 'Bob'}]
rgjs.array.fromObject({x: {name: 'Adam'}, y: {name: 'Bob'}});
// Convert an object to array using the `keyKey` argument.
// Returns [{__key__: 'x', name: 'Adam'}, {__key__: 'y', name: 'Bob'}]
rgjs.array.fromObject({x: {name: 'Adam'}, y: {name: 'Bob'}}, '__key__');

<static> getFirst(array)

Get the first value.

Parameters:
Name Type Description
array array

An array.

Returns:

The value or NULL if the array is empty.

Type
*
Example
rgjs.array.getFirst([1, 2, 3]); // 1

<static> getLast(array)

Get the last value.

Parameters:
Name Type Description
array array

An array.

Returns:

The value or NULL if the array is empty.

Type
*
Example
rgjs.array.getFirst([1, 2, 3]); // 3

<static> includes(array, lookup)

Check if an array contains a value.

Parameters:
Name Type Description
array array

An array.

lookup *

The value you're looking for.

Returns:

True if the array contains the value.

Type
boolean
Example
rgjs.array.includes([1, 2, 3], 2); // TRUE
rgjs.array.includes([1, 2, 3], 4); // FALSE

<static> isArray(val)

Check if a value is an array.

Parameters:
Name Type Description
val *

The value to check.

Returns:

True if 'val' is an array.

Type
boolean
Example
rgjs.array.isArray([1, 2, 3]); // TRUE
rgjs.array.isArray({x: 1, y: 2, z: 3}); // FALSE

<static> isArrayLike(val)

Check if a value is array-like.

Parameters:
Name Type Description
val *

The value to check.

Returns:

True if 'val' is an array.

Type
boolean
Example
rgjs.array.isArrayLike([1, 2, 3]); // TRUE
rgjs.array.isArrayLike(document.querySelector('p')); // TRUE
rgjs.array.isArray({x: 1, y: 2, z: 3}); // FALSE

<static> randomIndex(array)

Get a random index for an array.

Parameters:
Name Type Description
array array

An array.

Returns:

A random index for the array or -1.

Type
number
Example
rgjs.array.randomIndex(['a', 'b', 'c']); // Either 0, 1 or 2.

<static> randomVal(array)

Get a random value from an array.

Parameters:
Name Type Description
array array

An array.

Returns:

A random element from the array or NULL.

Type
*
Example
rgjs.array.randomIndex(['a', 'b', 'c']); // Either 'a', 'b' or 'c'

<static> removeVal(array, lookup)

Remove an array entry by value.

Parameters:
Name Type Description
array array

An array.

lookup *

The value you want to remove.

Returns:

The array.

Type
array
Example
rgjs.array.removeVal([1, 2, 3], 2); // [1, 3]