Security News
JavaScript Leaders Demand Oracle Release the JavaScript Trademark
In an open letter, JavaScript community leaders urge Oracle to give up the JavaScript trademark, arguing that it has been effectively abandoned through nonuse.
array.prototype.findlastindex
Advanced tools
An ESnext spec-compliant `Array.prototype.findLastIndex` shim/polyfill/replacement that works as far down as ES3.
The array.prototype.findlastindex npm package extends the Array prototype to include a findLastIndex method. This method allows you to search an array backwards and find the index of the last element that satisfies a provided testing function. It is particularly useful for finding the last occurrence of elements in arrays where elements may not be unique or when the order of elements is significant.
Finding the last index of an element that satisfies a condition
This feature allows you to find the index of the last element in an array that meets a specific condition. In the provided code sample, the condition is that the element must be greater than 13, which would return 4, the index of 44, as it is the last element satisfying the condition.
[5, 12, 8, 130, 44].findLastIndex(element => element > 13)
Lodash is a popular utility library that includes a findLastIndex method. This method offers similar functionality to array.prototype.findlastindex, allowing users to find the index of the last element in an array that satisfies a provided condition. The main difference is that lodash's findLastIndex is a standalone function that takes an array as its first argument, rather than being a method on the array prototype. This can be preferable for those who wish to avoid extending native prototypes or are working in environments where such modifications are discouraged.
An ESnext spec-compliant Array.prototype.findLastIndex
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.findLastIndex
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.findlastindex
var findLastIndex = require('array.prototype.findlastindex');
var assert = require('assert');
var arr = [1, [2], [], 3, [[4]]];
var isNumber = function (x) { return typeof x === 'number' };
assert.deepEqual(findLastIndex(arr, isNumber), 3);
var findLastIndex = require('array.prototype.findlastindex');
var assert = require('assert');
/* when Array#findLastIndex is not present */
delete Array.prototype.findLastIndex;
var shimmed = findLastIndex.shim();
assert.equal(shimmed, findLastIndex.getPolyfill());
assert.deepEqual(arr.findLastIndex(isNumber), findLastIndex(arr, isNumber));
var findLastIndex = require('array.prototype.findlastindex');
var assert = require('assert');
/* when Array#findLastIndex is present */
var shimmed = findLastIndex.shim();
assert.equal(shimmed, Array.prototype.findLastIndex);
assert.deepEqual(arr.findLastIndex(isNumber), findLastIndex(arr, isNumber));
Simply clone the repo, npm install
, and run npm test
v1.2.4 - 2024-02-05
call-bind
, define-properties
, es-abstract
, es-shim-unscopables
, get-intrinsic
b689455
es-errors
, so things that only need those do not need get-intrinsic
ca636e6
functions-have-names
b91692c
aud
, npmignore
, tape
4f87dff
hasown
instead of has
57ce292
object-inspect
, tape
fa74bab
FAQs
An ESnext spec-compliant `Array.prototype.findLastIndex` shim/polyfill/replacement that works as far down as ES3.
The npm package array.prototype.findlastindex receives a total of 15,127,371 weekly downloads. As such, array.prototype.findlastindex popularity was classified as popular.
We found that array.prototype.findlastindex demonstrated a healthy version release cadence and project activity because the last version was released less than 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
In an open letter, JavaScript community leaders urge Oracle to give up the JavaScript trademark, arguing that it has been effectively abandoned through nonuse.
Security News
The initial version of the Socket Python SDK is now on PyPI, enabling developers to more easily interact with the Socket REST API in Python projects.
Security News
Floating dependency ranges in npm can introduce instability and security risks into your project by allowing unverified or incompatible versions to be installed automatically, leading to unpredictable behavior and potential conflicts.