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

    Function arrLastIndexOfSubset

    • Function

      The arrLastIndexOfSubset() method returns the last index within theArray at which subset matches contiguously, comparing elements using strict equality (the same method used by the === or triple-equals operator). It is the mirror of arrIndexOfSubset, searching backwards from fromIndex (or the end of theArray) towards the start.

      As with arrIndexOfSubset, if theArray has fewer remaining elements than subset from a candidate position, the comparison is limited to just those available elements -- so a subset that "runs off the end" of theArray is still considered a match against its available prefix. As the search starts from the end, a partial match at the end is found before an earlier full match, so a trailing element which matches the first element of subset is returned, eg. arrLastIndexOfSubset([1, 2, 3, 1], [1, 2]) is 3 rather than 0. To only find a full match pass a fromIndex of theArray.length - subset.length, eg. arrLastIndexOfSubset([1, 2, 3, 1], [1, 2], 2) is 0. The elements are also read by index, so for a string its characters are compared and an empty slot of a sparse array is compared as undefined.

      Type Parameters

      • T

        Identifies the type of array elements

      Parameters

      • theArray: ArrayLike<T>

        The array or array-like object of elements to be searched.

      • subset: ArrayLike<T>

        The (ordered) array or array-like object of elements to locate within theArray.

      • OptionalfromIndex: number

        The index to start searching backwards from (the position at which subset's first element may occur). If the provided index value is a negative number, it is taken as the offset from the end of theArray. Default: theArray's length - 1 (search from the end).

      Returns number

      The last index within theArray at which subset fully matches (or matches theArray's available elements from that position), or -1 if no such position exists. Returns fromIndex (normalized to a valid index, or theArray's length) if subset is empty.

      0.20.0

      arrLastIndexOfSubset([1, 2, 3, 4, 5], [3, 4]);    // 2
      arrLastIndexOfSubset([1, 2, 3, 4, 5], [4, 3]); // -1
      arrLastIndexOfSubset(["a", "b", "a", "b", "c"], ["a", "b"]); // 2 (the later occurrence)
      arrLastIndexOfSubset([1, 2, 3], [3, 4, 5]); // 2 (matches the available "3")
      arrLastIndexOfSubset([1, 2, 3], []); // 3
      arrLastIndexOfSubset([1, 2, 3, 1], [1, 2]); // 3 (the trailing "1" is a partial match)
      arrLastIndexOfSubset([1, 2, 3, 1], [1, 2], 2); // 0 (only full matches, as 2 is length - subset length)

      // Repeated values -- returns the later occurrence, unlike arrIndexOfSubset()
      let haystack = ["at foo", "at bar", "at foo", "at bar", "at baz"];
      arrLastIndexOfSubset(haystack, ["at foo", "at bar"]); // 2