Ramda.js R.findLastIndex() Method

Beginner
5 min read
Updated: Nov 2026
5 examples
List

Definition

A helper that searches a list from the right and returns the position of the last item that passes a test. If no item passes, it returns -1. The index is a normal zero-based position counted from the left, and the list is never changed. It has been part of Ramda List since v0.1.1 (stable, not deprecated, experimental, or non-standard).

Why the index? You need a position to insert after, cut at, or update the last match, for example with R.insert, R.take, or R.adjust. If you only want the item, use R.findLast.

Syntax

Call R.findLastIndex. Start with this line.

Ramda
R.findLastIndex(pred, list)
CallWhat it does
R.findLastIndex(pred, list)Primary signature.
R.findLastIndex(...)Call the list helper.

Prefer import { findLastIndex } from "ramda" when you want a named import.

Library declarations
Ramda
R.findLastIndex(pred, list)

Parameters

ParameterDescription
predRequired. value → Boolean. Truthy means "this is the one"
listRequired. An array (or string) to search

Return Value

Return ValueDescription
*A number: the last matching index, or -1

Ramda Examples

Example 1 — Official Samples vs R.findIndex

Two objects match. The last one is at index 1, and the first is at index 0.

Ramda
const xs = [{ a: 1, b: 0 }, { a: 1, b: 1 }];

console.log(R.findLastIndex(R.propEq(1, "a"))(xs));
// → 1

console.log(R.findLastIndex(R.propEq(4, "a"))(xs));
// → -1

console.log(R.findIndex(R.propEq(1, "a"))(xs));
// → 0
Try It Yourself

How It Works

  1. Build the test. R.propEq(1, "a") checks whether obj.a equals 1. Both objects pass.
  2. From the right. findLastIndex checks index 1 first, it passes, and the result is 1.
  3. Miss. No object has a: 4, so the result is -1.
  4. From the left. R.findIndex meets index 0 first, so it returns 0.

Example 2 — Insert After the Last Match

Add a new high-priority job at the end of the high-priority group.

Ramda
const queue = [
  { p: "high", name: "a" },
  { p: "high", name: "b" },
  { p: "low",  name: "c" }
];

const i = R.findLastIndex(R.propEq("high", "p"), queue);
const next = R.insert(i + 1, { p: "high", name: "new" }, queue);

console.log(R.pluck("name", next));
// → ["a", "b", "new", "c"]
Try It Yourself

How It Works

  1. Locate. From the right, c is low and b is high, so i is 1.
  2. Insert after. R.insert(2, job, queue) places the job right after b.
  3. No mutation. next is a new array, and queue is unchanged.
  4. Miss is safe here. With no high jobs, i + 1 is 0, so the job goes to the front, which is still a sensible place.

Example 3 — Trim Trailing Empty Lines

Keep everything up to the last non-empty line.

Ramda
const lines = ["intro", "body", "", ""];

const last = R.findLastIndex(s => s !== "", lines);
console.log(last);
// → 1

console.log(R.take(last + 1, lines));
// → ["intro", "body"]
Try It Yourself

How It Works

  1. From the end. Index 3 and index 2 are empty strings, so they fail.
  2. Match. "body" at index 1 is the last non-empty line.
  3. Cut. R.take(2, lines) keeps indexes 0 and 1.
  4. Shortcut. R.dropLastWhile(R.equals(""), lines) gives the same result in one call when you don’t need the index.

Example 4 — Safely Update the Last Match

A reusable helper that leaves the list alone when nothing matches.

Ramda
const updateLast = (pred, fn, list) => {
  const i = R.findLastIndex(pred, list);
  return i === -1 ? list : R.adjust(i, fn, list);
};

const nums = [1, 8, 3, 6, 5];
const isEven = n => n % 2 === 0;
const times10 = n => n * 10;

console.log(updateLast(isEven, times10, nums));
// → [1, 8, 3, 60, 5]

console.log(updateLast(n => n > 100, times10, nums));
// → [1, 8, 3, 6, 5]
Try It Yourself

How It Works

  1. Hit. The last even number is 6 at index 3, so R.adjust(3, times10, nums) turns it into 60.
  2. First even untouched. 8 is also even, but it is not the last one.
  3. Miss. Nothing is over 100, so i is -1 and the helper returns the list unchanged.
  4. Why guard? R.adjust(-1, times10, nums) would count from the end and turn 5 into 50 by mistake.

Example 5 — findLastIndex vs lastIndexOf

Use lastIndexOf to find a known value and findLastIndex to find an item that meets a condition.

Ramda
const nums = [4, 9, 2, 9, 1];

console.log(R.lastIndexOf(9, nums));
// → 3   (last exact 9)

console.log(R.findLastIndex(n => n < 3, nums));
// → 4   (last item under 3)

const xs = [{ a: 1 }, { a: 1 }];

console.log(R.lastIndexOf({ a: 1 }, xs));
// → 1   (Ramda compares by value)

console.log(xs.lastIndexOf({ a: 1 }));
// → -1  (native compares by reference)
Try It Yourself

How It Works

  1. Known value. R.lastIndexOf(9, nums) finds the second 9 at index 3.
  2. Condition. R.findLastIndex(n => n < 3, nums) checks from the right, and 1 at index 4 passes first.
  3. Objects. R.lastIndexOf uses R.equals, so a fresh {a: 1} matches.
  4. Native. Array.prototype.lastIndexOf compares with ===, so a new object literal never matches.

Browser & Runtime Support

R.findLastIndex has been a Ramda List helper since v0.1.1. It uses its own backward loop, so it does not need the ES2023 Array.prototype.findLastIndex (Chrome 97+, Firefox 104+, Safari 15.4+, Node.js 18+). It runs anywhere Ramda runs.

✓ Stable · Ramda 0.1.1+

R.findLastIndex()

Support depends on the Ramda version you load, not on a browser feature. Your own test must also be valid in that runtime. For example, arrow functions need an ES2015 engine.

100% Modern browsers & runtimes
Google Chrome Modern versions · Desktop & Mobile
Full support
Mozilla Firefox Modern versions · Desktop & Mobile
Full support
Apple Safari Modern versions · macOS & iOS
Full support
Microsoft Edge Chromium-based
Full support
Opera Modern versions
Full support
Samsung Internet Modern versions · Android
Full support
Android WebView Modern WebView
Full support
Internet Explorer Not a target for current Ramda releases
Legacy
Node.js Maintained LTS · npm install ramda
Supported
Bun import from "ramda"
Supported
Deno import from "npm:ramda"
Supported
R.findLastIndex Ramda 0.1.1+

Bottom line: Safe for production in any modern browser or JavaScript runtime once Ramda is loaded, including engines that predate the native findLastIndex. The labs on this page use [email protected] from a CDN.

Frequently Asked Questions

R.findLastIndex(pred, list) searches from the right and returns the index of the last item for which pred returns a truthy value, or -1 if no item passes. Docs sample: with xs = [{a: 1, b: 0}, {a: 1, b: 1}], R.findLastIndex(R.propEq(1, 'a'))(xs) //=> 1.
No. The search runs right to left, but the index is a normal zero-based position counted from the left. In [5, 8, 8], the last 8 is at index 2.
R.lastIndexOf looks for a known value and compares with R.equals. R.findLastIndex looks for the last item that passes a test, such as n => n < 3.
Both return the last matching index or -1. The native method arrived in ES2023 (Chrome 97, Firefox 104, Safari 15.4, Node.js 18). R.findLastIndex works in any engine Ramda supports and is curried for R.pipe.
R.update, R.remove, R.adjust, and R.nth treat -1 as the last item. An unchecked miss silently edits or reads the last element. Compare with i !== -1 first.
No. R.findLastIndex has been part of Ramda since v0.1.1 and is stable. It is not deprecated, experimental, or non-standard.

Next: R.flatten()

Learn how to turn nested arrays into a single flat list.

R.flatten() tutorial →

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

6 people found this page helpful