Find the last matching index by function, shape, or key
A deploy log is an array of step records, newest last. When a pipeline fails, the dashboard jumps to the most recent step that matches a filter: the last failed step, the last step run by a given worker, the last step before a checkpoint. Implement findLastWhere(array, predicate, fromIndex), which returns the index of the last element that matches.
findLastWhere(array, predicate, fromIndex?) // => number
predicate comes in three forms:
predicate(value, index, array). The element matches when it returns a truthy value.element[key] === shape[key]. An empty shape matches every non-null object.element[key] is truthy.fromIndex down to index 0 and return the first index that matches. Return -1 if nothing matches.fromIndex defaults to array.length - 1. A negative fromIndex counts from the end (-1 is the last element). If it is still below 0 after that, return -1 without calling the predicate. If it is past the end, start at the last element.fromIndex is always an integer or undefined.undefined values.findLastIndex or findLast.const steps = [
{ name: 'install', status: 'ok', worker: 'w1' },
{ name: 'lint', status: 'failed', worker: 'w2', retried: true },
{ name: 'test', status: 'ok', worker: 'w1' },
{ name: 'build', status: 'failed', worker: 'w1' },
{ name: 'deploy', status: 'skipped', worker: 'w2' },
];
findLastWhere(steps, (step) => step.status === 'failed'); // => 3
findLastWhere(steps, { status: 'ok', worker: 'w1' }); // => 2
findLastWhere(steps, 'retried'); // => 1
findLastWhere(steps, { status: 'failed' }, 2); // => 1
findLastWhere(steps, { worker: 'w2' }, -2); // => 1
findLastWhere(steps, { status: 'cancelled' }); // => -1
findLastWhere([4, 9, 16], (n) => n > 100); // => -1