Class List

An ordered iterable collection. It optionally enforces the type of elements that may be added to the List.

An example usage:

  var list = new go.List(go.Point);  // make a list of Points
  list.add(new go.Point(0, 0));
  list.add(new go.Point(20, 10));
  list.add(new go.Point(10, 20));
  // now list.length === 3
  // and list.elt(1) instanceof go.Point

You can iterate over the items in a List:

  var it = aList.iterator;
  while (it.next()) {
    window.console.log("#" + it.key + " is " + it.value);
  }
Or:
  aList.each(function(val) {
      window.console.log(val);
    });
The key will range from zero to count-1.

For convenience this GoJS List class has synonyms for the following methods and property:

Constructor Summary Details

Name Description
List(type)

There are three possible constructors: List(), List(string) where string is a primitive type ('number', 'string', 'boolean', or 'function'), or List(func) where func is a class function/constructor, such as GraphObject.More...

Typical usage would be something like:

  var list = new go.List(go.GraphObject);  // keep a list of GraphObjects
Parameters:
{function(...)|string|null=} type
Optional class type (constructor) or a string describing a primitive type. 'string', 'number', 'boolean', and 'function' are accepted primitive types.

Properties Summary Details

Name, Value Type Description
count
{number}

This read-only property is the length of the List.

iterator
{Iterator.}

Gets an object that you can use for iterating over the List.More... The key will be an integer from zero to the count-1. The value will be the item at that index in the list. Typical usage:

  var it = aList.iterator;
  while (it.next()) {
    . . . "index: " + it.key + " value: " + it.value . . .
  }
iteratorBackwards
{Iterator.}

Gets an object that you can use for iterating over the List in backwards order.More... The key will be an integer from count-1 to zero. The value will be the item at that index in the list. The list is not modified by traversing in reverse order. Typical usage:

  var it = aList.iteratorBackwards;
  while (it.next()) {
    . . . 'key: ' + it.key + ' value: ' + it.value . . .
  }
length
{number}

This read-only property is the length of the List, a synonym for the count property.

Method Summary Details

Name, Return Type Description
add(val)

Adds a given value to the end of the List.More...

Be careful not to call this method while iterating over the collection.

Parameters:
val
addAll(coll)
{List.}

Adds all of the values of a collection to the end of this List.More...

Be careful not to call this method while iterating over the collection.

Parameters:
{Iterable.|Array} coll
the collection of items to add.
Returns:
{List.} this List.
all(pred)
{boolean} 1.4

This is true if all invocations of the given predicate on items in the collection are true.More...

Call the given predicate on each item in the collection. As soon as a call returns false, this returns false. Otherwise this returns true. For an empty collection this returns true.

Parameters:
{function(T):boolean} pred
This function must not have any side-effects.
Returns:
{boolean} True if all predicate calls are true; false otherwise.
any(pred)
{boolean} 1.4

This is true if any invocation of the given predicate on items in the collection is true.More...

Call the given predicate on each item in the collection. As soon as a call returns true, this returns true. Otherwise this returns false. For an empty collection this returns false.

Parameters:
{function(T):boolean} pred
This function must not have any side-effects.
Returns:
{boolean} True if any predicate call is true; false otherwise.
clear()

Clears the List.More... This sets the count to zero.

Be careful not to call this method while iterating over the collection.

contains(val)
{boolean}

Returns whether the given value is in this List.More...

Parameters:
{T} val
The value to check.
Returns:
{boolean} Whether or not the value is contained within the List.
copy()
{List.}

Makes a shallow copy of this List.More... The values are not copied, so if they are objects they may continue to be shared with the original List.

Returns:
{List.} The new list.
each(func)
{List} 1.4

Call the given function on each item in the collection.More...

Parameters:
{function(T)} func
This function must not modify the collection.
Returns:
{List} this list itself
elt(i)
{T}

Returns the element at the given index.More...

Parameters:
{number} i
int The index of the element to return.
Returns:
{T} the value at the given index.
first()
{?T}

Returns the first item in the list, or null if there is none.

Returns:
{?T} This returns null if there are no items in the list.
indexOf(val)
{number}

Returns the index of the given value if it is in this List.More...

Parameters:
{T} val
The value to check.
Returns:
{number} int returns -1 if the value is not in this list.
insertAt(i, val)

Insert a value before the index i.More...

Be careful not to call this method while iterating over the collection.

Parameters:
{number} i
int The index to insert before.
{T} val
The value to insert.
last()
{?T} 1.5

Returns the last item in the list, or null if these is none.

Returns:
{?T} This returns null if there are no items in the list.
pop()
{?T} 1.5

Returns the last item in the list and removes it from the list, or just return null if these is none.More... Use add to push an item onto the end of the list. Use last to get the last item without reducing the length of the list.

Returns:
{?T} This returns null if there are no items in the list.
remove(val)
{boolean}

Removes a given value (if found) from the List.More...

Be careful not to call this method while iterating over the collection.

Parameters:
{T} val
The value to remove.
Returns:
{boolean} true if the value was found and removed, false otherwise.
removeAt(i)

Removes a value at a given index from the List.More...

Be careful not to call this method while iterating over the collection.

Parameters:
{number} i
int The index to remove.
removeRange(to, from)

Removes a range of values from the List.More...

Be careful not to call this method while iterating over the collection.

Parameters:
{number} to
int The starting index of the range to remove, inclusive.
{number} from
int The ending index of the range to remove, inclusive.
reverse()
{List.}

Reverse the order of items in this List.

Returns:
{List.} this modified List.
setElt(i, val)

Set the element at the given index to a given value.More...

Parameters:
{number} i
int The index of the element to set.
{T} val
The value to set at the index.
sort(sortfunc)
{List.}

Sort the List according to a comparison function.More...

Parameters:
{function(*,*):number} sortfunc
This function is passed two items in the list. It should return zero if they are equal, less than zero if the first value should come before the second value, or greater than zero if the first value should come after the second value.
Returns:
{List.} this modified List.
toArray()
{Array.}

Produces a JavaScript Array from the contents of this List.

Returns:
{Array.} A copy of the List in Array form.
toSet()
{Set.}

Converts the List to a Set.More... The count of the resulting Set may be less than the count of this List if any duplicates were removed.

Returns:
{Set.} A copy of the contents of this List, but with duplicates removed and ordering lost.