1let arr = [
2 { name:"string 1", value:"this", other: "that" },
3 { name:"string 2", value:"this", other: "that" }
4];
5
6let obj = arr.find(o => o.name === 'string 1');
7
8console.log(obj);
1const object1 = {
2 a: {val: "aa"},
3 b: {val: "bb"},
4 c: {val: "cc"}
5};
6
7let a = Object.values(object1).find((obj) => {
8 return obj.val == "bb"
9});
10console.log(a)
11//Object { val: "bb" }
12//Use this for finding an item inside an object.
1// To find a specific object in an array of objects
2myObj = myArrayOfObjects.find(obj => obj.prop === 'something');
1var __POSTS = [
2 { id: 1, title: 'Apple', description: 'Description of post 1' },
3 { id: 2, title: 'Orange', description: 'Description of post 2' },
4 { id: 3, title: 'Guava', description: 'Description of post 3' },
5 { id: 4, title: 'Banana', description: 'Description of post 4' }
6];
7
8var __FOUND = __POSTS.find(function(post, index) {
9 if(post.title == 'Guava')
10 return true;
11});
12
13// On success __FOUND will contain the complete element (an object)
14// On failure it will contain undefined
15console.log(__FOUND); // { id: 3, title: 'Guava', description: 'Description of post 3' }
16
1var ages = [3, 10, 18, 20];
2
3function checkAdult(age) {
4 return age >= 18;
5}
6/* find() runs the input function agenst all array components
7 till the function returns a value
8*/
9ages.find(checkAdult);
10
1function isBigEnough(element) {
2 return element >= 15;
3}
4
5[12, 5, 8, 130, 44].find(isBigEnough); // 130