What is array.prototype.findlast?
The array.prototype.findlast npm package extends the Array prototype to include a `findLast` method. This method allows users to search through an array from the end towards the beginning, returning the last element that satisfies a provided testing function. It is particularly useful for finding the last occurrence of elements in an array that meet certain criteria.
What are array.prototype.findlast's main functionalities?
Finding the last element that meets a condition
This feature allows you to find the last element in an array that meets a specific condition. In the provided code sample, `findLast` is used to find the last even number in the array.
[1, 2, 3, 4, 5].findLast(element => element % 2 === 0)
Other packages similar to array.prototype.findlast
lodash.findlast
Lodash is a popular utility library that includes a `findLast` method. This method is similar to array.prototype.findlast in that it searches an array from the end and returns the last element that passes a given test. The main difference is that lodash's `findLast` is a standalone function that takes the array as an argument, rather than being a method on the array prototype.
underscore
Underscore.js is another utility library that offers similar functionality through its `findLast` method. Like lodash, it provides this functionality as a part of a larger set of utilities for working with arrays, objects, and functions. The approach to finding the last element that meets a condition is similar, but underscore does not modify the array prototype.
array.prototype.findlast
An ESnext spec-compliant Array.prototype.findLast
shim/polyfill/replacement that works as far down as ES3.
This package implements the es-shim API interface. It works in an ES3-supported environment and complies with the proposed spec.
Because Array.prototype.findLast
depends on a receiver (the this
value), the main export takes the array to operate on as the first argument.
Getting started
npm install --save array.prototype.findlast
Usage/Examples
var findLast = require('array.prototype.findlast');
var assert = require('assert');
var arr = [1, [2], [], 3, [[4]]];
var isNumber = function (x) { return typeof x === 'number' };
assert.deepEqual(findLast(arr, isNumber), 3);
var findLast = require('array.prototype.findlast');
var assert = require('assert');
delete Array.prototype.findLast;
var shimmed = findLast.shim();
assert.equal(shimmed, findLast.getPolyfill());
assert.deepEqual(arr.findLast(isNumber), findLast(arr, isNumber));
var findLast = require('array.prototype.findlast');
var assert = require('assert');
var shimmed = findLast.shim();
assert.equal(shimmed, Array.prototype.findLast);
assert.deepEqual(arr.findLast(isNumber), findLast(arr, isNumber));
Tests
Simply clone the repo, npm install
, and run npm test