End Times
Why should arrays have all the fun? you can add and remove values from the beginning and end of collections using push
,
pop
, shift
and unshift
.
With Collections, we use a more semantic set of methods to perform these tasks. Because some collection types have arbitrary keys, sometimes a second argument is present, being the key for the new value.
addBefore(item, key?)
: self
Adds the value to the beginning of the collection. This is analogous to the unshift(item)
method of arrays.
For Strings, Arrays, and Sets, only a single argument is required/used.
For Maps and Objects, two arguments are required; the second argument, the key, is required. Also, if the key already
exists in the collection it will replace the value previously stored in that key.
addAfter(item, key?)
: self
Adds the value to the end of the collection. This is analogous to the push(item)
method of arrays.
For Strings, Arrays, and Sets, only a single argument is required/used.
For Maps and Objects, two arguments are required; the second argument, the key, is required. Also, if the key already
exists in the collection it will replace the value previously stored in that key.
removeFirst(count?)
: item
Removes and returns the first (or first N) item of the collection -- order reflects the keys of the collection.
This is analogous to shift()
method of Arrays.
removeLast(count?)
: item
Removes and returns the last (or last N) item of the collection -- order reflects the keys of the collection.
This is analogous to pop()
method of Arrays.
first(count?: number)
: items[];
returns one(or more, with a count setting) values from the collection's items.
last(count?: number)
: items[];
returns one (or more) items from the end of the collection's items
firstItem
(a property) The first item in the collection
lastItem
the last item in the collection