Finding An Object By ID in an Array of Javascript Objects

Ole Ersoy
Ole Ersoy
Nov 3 · 1 min read
Image by Dimitris Christou from Pixabay

Scenario

We have a an array of objects:

const books = [
{'id':1, 'name': 'Removing Plastic from the Ocean'},
{'id':2,'name':'Universal Citizenship'}];

We want the find the name of the book with id 2 .

Approach

const name = books.find(x => x.id === 2).name;

Demo

Welcome to a place where words matter. On Medium, smart voices and original ideas take center stage - with no ads in sight. Watch
Follow all the topics you care about, and we’ll deliver the best stories for you to your homepage and inbox. Explore
Get unlimited access to the best stories on Medium — and support writers while you’re at it. Just $5/month. Upgrade