javascript-collections-framework
    Preparing search index...

    Function asList

    • Returns a list whose elements are array itself, without copying it. Every change made through the list — add, insert, removeAt, set, sort, clear, through its sub-lists, reversed view and cursors — writes to the array, and changes made to the array directly show through the list. A direct change of the array's length makes open iterators, cursors and sub-lists of the list throw ConcurrentModificationError on their next move; same-length direct writes show through but are not detected and do not change revision. The same holds for callbacks: a comparator, forEach action, removeIf predicate or replaceAll function that changes the array's length makes the call throw ConcurrentModificationError once it returns, and its writes stay; same-length writes are not detected, and sort writes its result over them.

      The list follows the list rules, not the array's: indexOf uses SameValueZero, equals compares with any list by elements, and sort is stable and throws NullElementError for null, undefined and holes in natural order (holes read as undefined). A frozen array refuses every write, and a sealed or non-extensible one every change of length, with TypeError before anything is written. Only those three states are checked: an element made non-writable or non-configurable with Object.defineProperty on an extensible array, or a Proxy array, can make a write fail part-way and leave elements shifted. Each call returns a new, independent list.

      Time: O(1). get, set and size are O(1), add amortised O(1), insert and removeAt O(n).

      Type Parameters

      • E

      Parameters

      • array: E[]

        The array to show as a list.

      Returns List<E>

      A list over array, printed as ArrayListView.

      TypeError when array is not an array (array-likes and typed arrays included).

      const scores = [30, 10, 20]
      const list = asList(scores)
      list.sort()
      list.add(40)
      scores // [10, 20, 30, 40]