Skip to content

Instantly share code, notes, and snippets.

@vanaf1979
Last active June 4, 2020 07:23
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 vanaf1979/ef1bcaa71e99308dd077b25f42792b40 to your computer and use it in GitHub Desktop.
Save vanaf1979/ef1bcaa71e99308dd077b25f42792b40 to your computer and use it in GitHub Desktop.
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