Skip to content

Array Manipulation

Arthur Guiot edited this page Aug 4, 2018 · 2 revisions

TheoremJS have a range of functions to help you manipulate arrays and lists.

flatten

This method will help you convert an array like [ [1, 2], [3, 4], 5 ] to [1, 2, 3, 4, 5]

Here is how you use it:

t.flatten([
    [1, 2],
    [3, 4], 5
]) // => [1, 2, 3, 4, 5]

linspace

This will create a range of number starting from the number a, going to the number b with x numbers in it.

If you don't have any idea of how it works, the example may help you:

t.linspace(0, 100, 10) // => [0, 10, 20, 30, 40, 50, 60, 70, 80, 90, 100]

range

This will create a range of number, starting from 0 up to a number x:

t.range(10) // => [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10]

arange

Similar as range, but with more options (start, end, step):

t.arange(1, 11, 2) // => [ 1, 3, 5, 7, 9, 11 ]

reshape

The reshape function is the opposite of the flatten function. It will group items in an array by x items:

t.reshape(t.range(10), 2) /* => [
    [0, 1],
    [2, 3],
    [4, 5],
    [6, 7],
    [8, 9],
    [10]
]) */

reverse

This function is almost the same as the native Array.prototype.reverse() function, but it's not destructive.

t.reverse([1, 2, 3]) // [3, 2, 1]
Clone this wiki locally