Skip to content

Instantly share code, notes, and snippets.

@prof3ssorSt3v3
Created November 25, 2022 19:46
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save prof3ssorSt3v3/f94365a7ca3f454966da684e548911f9 to your computer and use it in GitHub Desktop.
Save prof3ssorSt3v3/f94365a7ca3f454966da684e548911f9 to your computer and use it in GitHub Desktop.
Code for video on Array.prototype findLast and findLastIndex
// 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