Class List<T>

GoJS® Diagramming Components
version 3.0.13
by Northwoods Software®

An ordered iterable collection. In TypeScript it is a generic class that enforces at compile-time the type of elements that may be added to the List.

An example usage:

  const list = new go.List();  // or in TypeScript: new go.List<go.Point>();
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:

  const it = aList.iterator;
while (it.next()) {
console.log("#" + it.key + " is " + it.value);
}

Or:

  aList.each(val => {
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:

The constructor now takes an optional Iterable or Array argument that provides the initial elements for the new List.

Note that GoJS iteration is quite different than ECMAScript iteration, so that functionality has not been made somewhat compatible. These collection classes were defined in GoJS before the ECMAScript collection classes were proposed.

Type Parameters

  • T

Implements

Index

Constructors

  • There are two possible constructors:

    new go.List(), for JavaScript

    new go.List<type>() for TypeScript, to enforce type checking.

    Typical usage would be something like:

      const list = new go.List();  // keep a list of GraphObjects
    

    Type Parameters

    • T

    Parameters

    • Optional coll: T[] | Iterable<T>

      an optional collection of items to add.

    Returns List<T>

Accessors

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

  • Gets an object that you can use for iterating over the List. 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:

      const it = aList.iterator;
    while (it.next()) {
    . . . "index: " + it.key + " value: " + it.value . . .
    }
  • Gets an object that you can use for iterating over the List in backwards order. 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:

      const it = aList.iteratorBackwards;
    while (it.next()) {
    . . . 'key: ' + it.key + ' value: ' + it.value . . .
    }
  • This read-only property is the length of the List, a synonym for the count property.

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

Methods

  • This class implements the JavaScript Symbol.iterator, and can be used with spread syntax or for ... of statements.

    Returns IterableIterator<T>

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

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

    Parameters

    • val: T

    Returns List<T>

    This modified List.

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

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

    Parameters

    • coll: T[] | Iterable<T>

      the collection of items to add.

    Returns List<T>

    This modified List.

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

    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

    • pred: ((a: T) => boolean)

      This function must not have any side-effects.

        • (a: T): boolean
        • Parameters

          • a: T

          Returns boolean

    Returns boolean

    True if all predicate calls are true; false otherwise.

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

    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

    • pred: ((a: T) => boolean)

      This function must not have any side-effects.

        • (a: T): boolean
        • Parameters

          • a: T

          Returns boolean

    Returns boolean

    True if any predicate call is true; false otherwise.

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

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

    Returns void

  • Returns whether the given value is in this List.

    Parameters

    • val: T

      The value to check.

    Returns boolean

    Whether or not the value is contained within the List.

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

    Returns List<T>

    The new List with the same elements.

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

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

    Parameters

    • val: T

      The value to remove.

    Returns boolean

    true if the value was found and removed, false otherwise.

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

    Parameters

    • func: ((a: T) => void)

      This function must not modify the collection.

        • (a: T): void
        • Parameters

          • a: T

          Returns void

    Returns List<T>

    This List itself

  • Returns the element at the given index.

    Parameters

    • i: number

      int The index of the element to return.

    Returns T

    the value at the given index.

  • 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.

  • Returns the element at the given index.

    Parameters

    • i: number

      int The index of the element to return.

    Returns T

    the value at the given index.

  • Returns whether the given value is in this List.

    Parameters

    • val: T

      The value to check.

    Returns boolean

    Whether or not the value is contained within the List.

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

    Parameters

    • val: T

      The value to check.

    Returns number

    returns -1 if the value is not in this list.

  • Insert a value before the index i.

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

    Parameters

    • i: number

      int The index to insert before.

    • val: T

      The value to insert.

    Returns void

  • 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.

  • Returns the last item in the list and removes it from the list, or just return null if these is none. 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.

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

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

    Parameters

    • val: T

    Returns void

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

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

    Parameters

    • val: T

      The value to remove.

    Returns boolean

    true if the value was found and removed, false otherwise.

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

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

    Parameters

    • i: number

      int The index to remove.

    Returns void

  • Removes a range of values from the List, given both the starting and the ending zero-based indexes. For example,

    list.removeRange(2, 4)
    

    will remove elements 2, 3, and 4 from the list. If there were two or fewer elements in the list to begin with, the list is unchanged. If from is greater than to, the list is unchanged. If from is greater than or equal to the length, the list is unchanged. If to is less than zero, the list is unchanged.

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

    Parameters

    • from: number

      int The starting index of the range to remove, inclusive; negative values are treated as zero

    • to: number

      int The ending index of the range to remove, inclusive; values greater than the length of the list are treated as referring to the last element

    Returns List<T>

    This modified List

  • Reverse the order of items in this List.

    Returns List<T>

    This modified List.

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

    Parameters

    • i: number

      int The index of the element to set.

    • val: T

      The value to set at the index.

    Returns void

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

    Parameters

    • i: number

      int The index of the element to set.

    • val: T

      The value to set at the index.

    Returns void

  • Sort the List according to a comparison function.

    Parameters

    • sortfunc: ((a: T, b: T) => number)

      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.

        • (a: T, b: T): number
        • Parameters

          • a: T
          • b: T

          Returns number

    Returns List<T>

    This modified List.

  • Produces a JavaScript Array from the contents of this List.

    Returns T[]

    A copy of the List in Array form.

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

    Returns Set<T>

    A copy of the contents of this List, but with duplicates removed and ordering lost.