Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
How to use Array.prototype.find() to search for a single object in an array/api response.
// MDN Docs: https://developer.mozilla.org/nl/docs/Web/JavaScript/Reference/Global_Objects/Array/find
const friends = [
{ id: 0, name: "joey", quote: "How you doin?" },
{ id: 1, name: "ross", quote: "We were on a break" },
{ id: 2, name: "phoebe", quote: "She’s your lobster" }
];
const findFriendById = id => {
return friends.find(friend => friend.id === id);
};
console.log(findFriendById(0)); // Object {id: 0, name: "joey", quote: "How you doin?"}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.