Last active
June 4, 2020 07:23
-
-
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.
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
// 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