javascript search array of objects by id code example

Example 1: js get object by id from array

myArray.find(x => x.id === '45');

Example 2: javascript find item in array of objects

var __POSTS = [ 
	{ id: 1, title: 'Apple', description: 'Description of post 1' }, 
	{ id: 2, title: 'Orange', description: 'Description of post 2' }, 
	{ id: 3, title: 'Guava', description: 'Description of post 3' }, 
	{ id: 4, title: 'Banana', description: 'Description of post 4' }
];

var __FOUND = __POSTS.find(function(post, index) {
	if(post.title == 'Guava')
		return true;
});

// On success __FOUND will contain the complete element (an object)
// On failure it will contain undefined  
console.log(__FOUND); // { id: 3, title: 'Guava', description: 'Description of post 3' }

Example 3: javascript get array object by id

myArray.find(x => x.id === '45').foo;

Example 4: search an array of objects with specific object property value

var result = jsObjects.find(obj => {
  return obj.b === 6
})