// .find method only accepts a callback function // and returns the first value for which the function is true.const cities =["Orlando","Dubai","Denver","Edinburgh","Chennai","Accra","Denver","Eskisehir",];constfindCity=(element)=>{return element ==="Denver";};console.log(cities.find(findCity));//Expected output is 4 (first instance of "Denver" in array)
const inventory =[{name:'apples',quantity:2},{name:'bananas',quantity:0},{name:'cherries',quantity:5}];functionisCherries(fruit){return fruit.name==='cherries';}console.log(inventory.find(isCherries));// { name: 'cherries', quantity: 5 }/* find মেথড অলওয়েজ অ্যারের সরাসরি ভ্যালু রিটার্ণ করে।
অর্থাৎ কন্ডিশনের সাথে মিলে যাওয়ার পরে যে কারণে মিলসে সেই লজিক অনুযায়ী
ঐ অ্যারে থেকে প্রথম ভ্যালুটা রিটার্ণ করে। সে কারণে এখানে অ্যারের পুরো ভ্যালুটা আউটপুট হিসেবে দেখাচ্ছে। */// Same Code Using Arrow function & destructuring techniqueconst inventory =[{name:'apples',quantity:2},{name:'bananas',quantity:0},{name:'cherries',quantity:5}];const result = inventory.find(({ name })=> name ==='cherries');// ({name})==> destructuring techniqueconsole.log(result);// { name: 'cherries', quantity: 5 }
//find is higher oder function that return only if condition is trueconst names=["Shirshak","SabTech","Kdc"]const searchName = names.find(function(name){return names.inclues("Shirshak")})
str.indexOf("locate");// return location of first find value
str.lastIndexOf("locate");// return location of last find value
str.indexOf("locate",15);// start search from location 15 and then take first find value
str.search("locate");//The search() method cannot take a second start position argument. //The indexOf() method cannot take powerful search values (regular expressions).
/* This method returns first element of the array that satisfies the condition
specified in the callback function.
*/const x =[1,2,3,4,5];const y = x.find(el=> el*2===2);console.log("y is: ", y);// y is: 1/* Now, if you see the output of the above example, the value of y is 1.
This is because the find() method searches for first element in the array
that satisfies the condition specified.
*/