@nevware21/ts-utils
    Preparing search index...

    Function arrSlice

    The arrSlice() method returns a shallow copy of a portion of an array into a new array object selected from start to end (end not included) where start and end represent the index of items in that array. The original array will not be modified.

    The arrSlice() method is a copying method. It does not alter this but instead returns a shallow copy that contains some of the same elements as the ones from the original array.

    The arrSlice() method preserves empty slots. If the sliced portion is sparse, the returned arra is sparse as well.

    The arrSlice() method is generic. It only expects the this value to have a length property and integer-keyed properties.

    For both start and end, a negative index can be used to indicate an offset from the end of the array. For example, -2 refers to the second to last element of the array.

    When the value has its own slice function it is used (like theArray.slice(start, end)), otherwise Array.prototype.slice is called with the value as this. So for a string the result is a string, and for a value whose own slice returns its own type, such as a typed array, the result is that type (and for a Node Buffer it is a view of the same memory).

    0.9.3

    The array or array-like value to extract the elements from.

    Zero-based index at which to start extraction, converted to an integer.

    • Negative index counts back from the end of the array — if start < 0, start + array.length is used.
    • If start < -array.length or start is omitted, 0 is used.
    • If start >= array.length, nothing is extracted.

    Zero-based index at which to end extraction, converted to an integer. slice() extracts up to but not including end.

    • Negative index counts back from the end of the array — if end < 0, end + array.length is used.
    • If end < -array.length, 0 is used.
    • If end >= array.length or end is omitted, array.length is used, causing all elements until the end to be extracted.
    • If end is positioned before or at start after normalization, nothing is extracted.

    A new array containing the extracted elements, or the result of the value's own slice function.

    const lyrics = ["Hello", "Darkness", "my", "old", "friend.", "I've", "come", "to", "talk" ];

    arrSlice(lyrics); // [ "Hello", "Darkness", "my", "old", "friend.", "I've", "come", "to", "talk" ]
    arrSlice(lyrics, 1, 3); // [ "Darkness", "my" ]
    arrSlice(lyrics, 2); // [ "my", "old", "friend.", "I've", "come", "to", "talk" ]
    arrSlice(lyrics, 2, 4); // [ "my", "old" ]
    arrSlice(lyrics, 1, 5); // [ "Darkness", "my", "old", "friend." ]
    arrSlice(lyrics, -2); // [ "to", "talk" ]
    arrSlice(lyrics, 2, -1); // [ "my", "old", "friend.", "I've", "come", "to" ]

    // A value with its own slice function, which returns its own type
    arrSlice(new Uint8Array([1, 2, 3]), 1); // Uint8Array [ 2, 3 ]
    arrSlice("Hello", 1, 3); // "el"
    • Type Parameters

      • T extends object & { slice(start?: number, end?: number): any }

      Parameters

      • theArray: T
      • Optionalstart: number
      • Optionalend: number

      Returns 0 extends 1 & T
          ? any[]
          : T extends { slice(start?: number, end?: number): R } ? R : never

    • Parameters

      • theArray: string
      • Optionalstart: number
      • Optionalend: number

      Returns string

    • Type Parameters

      • T

      Parameters

      • theArray: ArrayLike<T>
      • Optionalstart: number
      • Optionalend: number

      Returns T[]