Created
November 25, 2022 19:46
-
-
Save prof3ssorSt3v3/f94365a7ca3f454966da684e548911f9 to your computer and use it in GitHub Desktop.
Code for video on Array.prototype findLast and findLastIndex
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Array.prototype.find() node v4.9.1 | |
// Array.prototype.findIndex() v4.9.1 | |
// Array.prototype.findLast() node v18.12.1+ | |
// Array.prototype.findLastIndex() node v18.12.1+ | |
const names = [ | |
{ name: 'Rachel' }, | |
{ name: 'Ross' }, | |
{ name: 'Chandler' }, | |
{ name: 'Phoebe' }, | |
{ name: 'Joey' }, | |
{ name: 'Monica' }, | |
{ name: 'Julian' }, | |
{ name: 'Bubbles' }, | |
{ name: 'Ricky' }, | |
]; | |
const bestFriend = 'Bubbles'; | |
function findAFriend(obj, index) { | |
//looking for a match between `bestFriend` and `name`. | |
console.log(index); | |
if (obj.name === bestFriend) return true; | |
//undefined | |
} | |
let n = names.findLast(findAFriend); | |
let i = names.findLastIndex(findAFriend); | |
console.log(n, i); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment