Array

These are extensions to JavaScript's Array. See this documentation for all standard Array methods. All of the methods that Rulecube has added return an array leave the original array intact and return a new array.

// To get the sum of the value of every real estate in London, you might use this:
realEstates
   .where(re => re.City === 'London')
   .sum(re => re.Value)

Methods

where

where(predicate: Function)

Filters based on the given predicate and returns the filtered array. Essentially an alias for filter().

Parameters

predicate Function

Predicate function for filtering data. This function is called with 3 parameters: value, index and the array itself.

Return type

array

Examples

persons.where(p => p.City === 'London')        // A new array containing only people who live in London
[1,12,300,6000].where(p => p > 100)            // [300, 6000] - filtered based on the value
[1,12,300,6000].where((p, idx) => idx > 1)     // [300, 6000] - filtered based on the index

whereIn

Filters the collection based cross-checking every item using the predicate, with another list. Also known as an Intersect.

Parameters

predicate Function

(optional) Predicate for selecting the data. This function is called with 3 parameters: value, index and the array itself.

otherList array

The other list to compare with.

Return type

array

Examples

whereNotIn

This is the inverse operation of the whereIn. Also known as an Except.

Parameters

predicate Function

(optional) Predicate for selecting the data. This function is called with 3 parameters: value, index and the array itself.

otherList array

The other list to compare with.

Return type

array

Examples

ofType

Filters based on the given type and returns the filtered typed array.

Parameters

type Type

The type to filter by.

Return type

array

Examples

map

Transforms the collection based on the given predicate. Actually one of the Array standard methods, but included here for reference. See also: map().

Parameters

predicate Function

Predicate for transforming/mapping the data. This function is called with 3 parameters: value, index and the array itself.

Return type

array

Examples

orderBy

Sorts the collection in an ascending order.

Parameters

predicate Function

(optional) Predicate for ordering data. This function is called with 1 parameter: value.

Return type

array

Examples

orderByDesc

Sorts the collection in an descending order.

Parameters

predicate Function

(optional) Predicate for ordering data. This function is called with 1 parameter: value.

Return type

array

Examples

copy

Create a (shallow) copy of the collection. Note: functions the same as .slice().

Parameters

Return type

array

Examples

reverse

Reverses the collection. Actually one of the Array standard methods, but included here for reference. See also: reverse(). Note: this changes the order of the array and does not create a copy! Use .copy().reverse() or .reverseCopy() for that instead.

Parameters

Return type

array

Examples

reverseCopy

Reverses a copy of the collection, leaves the original intact. Note: This is different from the native: reverse().

Parameters

Return type

array

Examples

take

Reduces the collection to the given amount of items from the start.

Parameters

num number

The number of items to take.

Return type

array

Examples

skip

Reduces the collection by removing the given amount of items.

Parameters

num number

The number of items to skip.

Return type

array

Examples

distinct

Reduces the collection to contain only unique values.

Parameters

predicate Function

(optional) Predicate for comparing the data.

Return type

array

Examples

groupBy

Groups the collection based on the given predicate.

Parameters

predicate Function

Predicate for grouping the data.

Return type

array

Examples

groupMap

Groups and maps the collection based on the given predicates.

Parameters

groupPredicate Function

Predicate for grouping the data.

mapPredicate Function

Predicate for transforming/mapping the data.

Return type

array

Examples

find

Finds an item in the collection based on the given predicate and returns the first item that matches. Actually one of the Array standard methods, but included here for reference. See also: find().

Parameters

predicate Function

Predicate for finding the item. This function is called with 3 parameters: value, index and the array itself.

Return type

any

Examples

findMin

Returns the item with the lowest-valued numeric value in the collection. A predicate may be used to transform/map the data. Note: This is similar to min() but returns the item instead of the value. See also:

  • Array.min

Parameters

predicate Function

(optional) Predicate for transforming/mapping the data.

Return type

T

Examples

findMax

Returns the item with the highest-valued numeric value in the collection. A predicate may be used to transform/map the data. Note: This is similar to max() but returns the item instead of the value. See also:

  • Array.max

Parameters

predicate Function

(optional) Predicate for transforming/mapping data.

Return type

T

Examples

first

Returns the first item in the collection. When given a predicate, finds and returns the first item that matches, similar to find().

Parameters

predicate Function

(optional) Predicate for finding the item. When not provided, this method will return the first item in the collection.

Return type

T

Examples

last

Returns the last item in the collection. When given a predicate, finds and returns the last item that matches.

Parameters

predicate Function

(optional) Predicate for finding the item. When not provided, this method will return the last item in the collection.

Return type

T

Examples

sum

Computes the sum of the collection based on the given predicate.

Parameters

predicate Function

(optional) Predicate for selecting data.

Return type

number

Examples

min

Returns the lowest-valued numeric value in the collection. A predicate may be used to transform/map the data. See also:

  • Array.findMin

Parameters

predicate Function

(optional) Predicate for transforming/mapping the data.

Return type

number

Examples

max

Returns the highest-valued numeric value in the collection. A predicate may be used to transform/map the data. See also:

  • Array.findMax

Parameters

predicate Function

(optional) Predicate for transforming/mapping data.

Return type

number

Examples

avg

Computes the average value of the collection based on the given predicate.

Parameters

predicate Function

(optional) Predicate for selecting data.

Return type

number

Examples

any

Returns whether any item in the collection matches the given predicate. Essentially an alias for some().

Parameters

predicate Function

(optional) Predicate function for evaluating data. This function is called with 3 parameters: value, index and the array itself.

Return type

boolean

Examples

all

Returns whether every item in the collection matches the given predicate. Essentially an alias for every().

Parameters

predicate Function

(optional) Predicate for matching data.

Return type

boolean

Examples

none

Returns whether none of the items in the collection matches the given predicate. The inverse of any().

Parameters

predicate Function

(optional) Predicate function for evaluating data. This function is called with 3 parameters: value, index and the array itself.

Return type

boolean

Examples

contains

Returns whether the given item is included in the collection. Essentially an alias for includes().

Parameters

item any

The item to look for in the collection.

Return type

number

Examples

count

Filters the collection based on the given predicate and returns the number of items left.

Parameters

predicate Function

(optional) Predicate for filtering data.

Return type

number

Examples

to

Casts collection to a different type. Has no effect during run-time, but helps with autocompletion.

Parameters

otherType any

Type to cast to.

Return type

array

Examples

Last updated