New in JavaScript: Array.findLast and Array.findLastIndex (#note)

An update of MDN’s browser-compat-data caught my eye today. Finding values in Arrays is a common practice via find and findIndex. These methods iterate from the array beginning, though.
const things = [{v: 1}, {v: 2}, {v: 3}, {v: 4}…


This content originally appeared on Stefan Judis Web Development and was authored by Stefan Judis

An update of MDN's browser-compat-data caught my eye today. Finding values in Arrays is a common practice via find and findIndex. These methods iterate from the array beginning, though.

const things = [{v: 1}, {v: 2}, {v: 3}, {v: 4}, {v: 5}];

things.find(elem => elem.v > 3); // {v: 4}
things.findIndex(elem => elem.v > 3); // 3

If you wanted to search your array starting from the end, you had to reverse the array and use the provided methods. That's not great because it requires an unnecessary array mutation.

Luckily, there's an ECMAscript proposal for findLast and findLastIndex.

const things = [{v: 1}, {v: 2}, {v: 3}, {v: 4}, {v: 5}];

things.findLast(elem => elem.v > 3); // {v: 5}
things.findLastIndex(elem => elem.v > 3); // 4

The proposal is currently on Stage 3 and will be implemented in Chromiums and Safari soon. For the rest, core-js and Babel already provide a polyfill.

That's a sweet little language addition. Go JavaScript!


Reply to Stefan


This content originally appeared on Stefan Judis Web Development and was authored by Stefan Judis


Print Share Comment Cite Upload Translate Updates
APA

Stefan Judis | Sciencx (2021-11-19T23:00:00+00:00) New in JavaScript: Array.findLast and Array.findLastIndex (#note). Retrieved from https://www.scien.cx/2021/11/19/new-in-javascript-array-findlast-and-array-findlastindex-note/

MLA
" » New in JavaScript: Array.findLast and Array.findLastIndex (#note)." Stefan Judis | Sciencx - Friday November 19, 2021, https://www.scien.cx/2021/11/19/new-in-javascript-array-findlast-and-array-findlastindex-note/
HARVARD
Stefan Judis | Sciencx Friday November 19, 2021 » New in JavaScript: Array.findLast and Array.findLastIndex (#note)., viewed ,<https://www.scien.cx/2021/11/19/new-in-javascript-array-findlast-and-array-findlastindex-note/>
VANCOUVER
Stefan Judis | Sciencx - » New in JavaScript: Array.findLast and Array.findLastIndex (#note). [Internet]. [Accessed ]. Available from: https://www.scien.cx/2021/11/19/new-in-javascript-array-findlast-and-array-findlastindex-note/
CHICAGO
" » New in JavaScript: Array.findLast and Array.findLastIndex (#note)." Stefan Judis | Sciencx - Accessed . https://www.scien.cx/2021/11/19/new-in-javascript-array-findlast-and-array-findlastindex-note/
IEEE
" » New in JavaScript: Array.findLast and Array.findLastIndex (#note)." Stefan Judis | Sciencx [Online]. Available: https://www.scien.cx/2021/11/19/new-in-javascript-array-findlast-and-array-findlastindex-note/. [Accessed: ]
rf:citation
» New in JavaScript: Array.findLast and Array.findLastIndex (#note) | Stefan Judis | Sciencx | https://www.scien.cx/2021/11/19/new-in-javascript-array-findlast-and-array-findlastindex-note/ |

Please log in to upload a file.




There are no updates yet.
Click the Upload button above to add an update.

You must be logged in to translate posts. Please log in or register.