
Security News
Feross on Risky Business Weekly Podcast: npm’s Ongoing Supply Chain Attacks
Socket CEO Feross Aboukhadijeh joins Risky Business Weekly to unpack recent npm phishing attacks, their limited impact, and the risks if attackers get smarter.
array.prototype.findlast
Advanced tools
An ESnext spec-compliant `Array.prototype.findLast` shim/polyfill/replacement that works as far down as ES3.
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.
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)
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.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.
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.
npm install --save array.prototype.findlast
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');
/* when Array#findLast is not present */
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');
/* when Array#findLast is present */
var shimmed = findLast.shim();
assert.equal(shimmed, Array.prototype.findLast);
assert.deepEqual(arr.findLast(isNumber), findLast(arr, isNumber));
Simply clone the repo, npm install
, and run npm test
FAQs
An ESnext spec-compliant `Array.prototype.findLast` shim/polyfill/replacement that works as far down as ES3.
We found that array.prototype.findlast demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
Socket CEO Feross Aboukhadijeh joins Risky Business Weekly to unpack recent npm phishing attacks, their limited impact, and the risks if attackers get smarter.
Product
Socket’s new Tier 1 Reachability filters out up to 80% of irrelevant CVEs, so security teams can focus on the vulnerabilities that matter.
Research
/Security News
Ongoing npm supply chain attack spreads to DuckDB: multiple packages compromised with the same wallet-drainer malware.