1const inventory = [
2 {name: 'apples', quantity: 2},
3 {name: 'cherries', quantity: 8}
4 {name: 'bananas', quantity: 0},
5 {name: 'cherries', quantity: 5}
6 {name: 'cherries', quantity: 15}
7
8];
9
10const result = inventory.find( ({ name }) => name === 'cherries' );
11
12console.log(result) // { name: 'cherries', quantity: 5 }
1const array1 = [5, 12, 8, 130, 44];
2
3const found = array1.find(element => element > 10);
4
5console.log(found);
6// expected output: 12
7
1const inventory = [
2 {name: 'apples', quantity: 2},
3 {name: 'bananas', quantity: 0},
4 {name: 'cherries', quantity: 5}
5];
6
7function isCherries(fruit) {
8 return fruit.name === 'cherries';
9}
10
11console.log(inventory.find(isCherries));
12// { name: 'cherries', quantity: 5 }
1const inventory = [
2 {name: 'apples', quantity: 2},
3 {name: 'bananas', quantity: 0},
4 {name: 'cherries', quantity: 5}
5];
6
7function isCherries(fruit) {
8 return fruit.name === 'cherries';
9}
10
11console.log(inventory.find(isCherries)); // { name: 'cherries', quantity: 5 }
12
13/* find মেথড অলওয়েজ অ্যারের সরাসরি ভ্যালু রিটার্ণ করে।
14অর্থাৎ কন্ডিশনের সাথে মিলে যাওয়ার পরে যে কারণে মিলসে সেই লজিক অনুযায়ী
15ঐ অ্যারে থেকে প্রথম ভ্যালুটা রিটার্ণ করে। সে কারণে এখানে অ্যারের পুরো ভ্যালুটা আউটপুট হিসেবে দেখাচ্ছে। */
16
17// Same Code Using Arrow function & destructuring technique
18
19const inventory = [
20 {name: 'apples', quantity: 2},
21 {name: 'bananas', quantity: 0},
22 {name: 'cherries', quantity: 5}
23];
24
25const result = inventory.find( ({ name }) => name === 'cherries' ); // ({name})==> destructuring technique
26
27console.log(result); // { name: 'cherries', quantity: 5 }
28
1const inventory = [
2 {name: 'apples', quantity: 2},
3 {name: 'cherries', quantity: 8}
4 {name: 'bananas', quantity: 0},
5 {name: 'cherries', quantity: 5}
6 {name: 'cherries', quantity: 15}
7
8];
9
10const result = inventory.find( ({ name }) => name === 'cherries' );
11
12console.log(result) // { name: 'cherries', quantity: 5 }
13