Arrays
ixfx has a host of functions for working with arrays. See the link above for a complete overview. On this page we’ll give a quick overview of working with arrays in Javascript interleaved with some ixfx functions.
In Javascript, arrays are essentially stacks, with items stored ‘left-to-right’ in the order added. Arrays can hold whatever you want, sometimes we even use arrays to hold other arrays, a way of making a multi-dimensional data structure like a table.
Arrays in Javascript are mutable meaning their contents can change even though the object reference stays the same. In this document, we’ll only use them in an immutable way.
Basics
Creating
Arrays are simple to create
Create an array from an iterable or generator
Adding
When working with immutable arrays, we don’t actually add anything to an array, but rather create a new array with previous elements and any new elements.
Use destructuring to add element(s) to an array. Where you put the destructured values will determine their position. Let’s say ‘a’ is the initial array.
Note in these cases, a
never changes.
Filter
To filter is to go through every item in an array and see if it matches a condition, called a predicate. If the predicate returns true, the item is returned, forming a new array. The original array is not changed.
In the above example we’re using a succint way to express the predicate. But it could be expanded as below, just make sure it returns true or false.
filter
will return an empty array if no matching items were found.
Removing items
When working with immutable arrays, we don’t really remove items, but rather create a new array without certain items. filter comes in handy for this.
For example, we want an array without carrot
:
If it seems ugly to always create new variables when using immutable arrays, use let
rather than const
:
Accessing contents
You can ‘loop over’ or ‘enumerate’ the contents of an array.
Items in arrays have an integer index, which starts at 0. Meaning the first item is at position in 0. Use at
to get an item by index. If you use negative numbers it will count from the last element.
Includes
filter can be used to find items that match a predicate and return a new array. However there are times you just want to check if something exists, without getting it back.
some
allows you to use a filtering predicate:
find
allows you to use a filtering predicate, returning the first item that matches.
find
will return undefined if no matching item was found.
Random
ixfx has functions for getting a random element or index:
weightedSource can be used for skewing the distributing of random elements, eg. to favour picking elements at the end of the array over elements at the beginning.
Remove a random element from an array with randomPluck . It doesn’t modify the array, but returns the randomly selected item and a new array without it.
Sorting
ixfx has sorting functions for common needs:
Grouping
groupBy allows you to group an array by some generated key.
This will yield the Map: