javascript find method in array of objects code example
Example 1: find in array of objects javascript
let arr = [
{ name:"string 1", value:"this", other: "that" },
{ name:"string 2", value:"this", other: "that" }
];
let obj = arr.find(o => o.name === 'string 1');
console.log(obj);
Example 2: find element in array javascript
const simpleArray = [3, 5, 7, 15];
const objectArray = [{ name: 'John' }, { name: 'Emma' }]
console.log( simpleArray.find(e => e === 7) )
console.log( simpleArray.find(e => e === 10) )
console.log( objectArray.find(e => e.name === 'John') )
Example 3: 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;
});
console.log(__FOUND);