insert Method

Attempts to insert a new value into the array at a position determined by the ordering. The behavior differs based on whether or not duplicate elements are permitted. If duplicates are not permitted, then:

  • If an equivalent element already exists in the array, nothing will be inserted and the index of the existing element will be returned.
  • Otherwise, the element is inserted and its index is returned. If duplicates are permitted, then:
  • The element will be inserted in a correct position based on the sorting criterion;
  • The position of the element relative to other elements comparing as equal to it is unspecified; and
  • The actual index of the newly-inserted element is returned. If the element is to be inserted, then the supplied value will be passed to the clone function supplied to the constructor and the result will be inserted into the array.

insert(value: T, onInsert?: (value: T) => any): number

Parameter Type Description
value T The value to insert
onInsert (value: T) => any The optional callback method to call if insertion occurs with the inserted value

Returns - number

the index in the array of the newly-inserted value, or, if duplicates are not permitted and an equivalent value already exists, the index of the equivalent value.

Defined in

Last Updated: 20 June, 2023