Function
Identifies the type of array elements
The array or array-like object of elements to be searched.
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).
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.
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
The arrLastIndexOfSubset() method returns the last index within
theArrayat whichsubsetmatches contiguously, comparing elements using strict equality (the same method used by the === or triple-equals operator). It is the mirror of arrIndexOfSubset, searching backwards fromfromIndex(or the end of theArray) towards the start.As with arrIndexOfSubset, if
theArrayhas fewer remaining elements thansubsetfrom a candidate position, the comparison is limited to just those available elements -- so asubsetthat "runs off the end" oftheArrayis 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 ofsubsetis returned, eg.arrLastIndexOfSubset([1, 2, 3, 1], [1, 2])is3rather than0. To only find a full match pass afromIndexoftheArray.length - subset.length, eg.arrLastIndexOfSubset([1, 2, 3, 1], [1, 2], 2)is0. 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 asundefined.