xxxxxxxxxx
const array1 = [5, 12, 8, 130, 44];
const found = array1.find(element => element > 10);
console.log(found);
// expected output: 12
xxxxxxxxxx
const inventory = [
{name: 'apples', quantity: 2},
{name: 'cherries', quantity: 8}
{name: 'bananas', quantity: 0},
{name: 'cherries', quantity: 5}
{name: 'cherries', quantity: 15}
];
const result = inventory.find( ({ name }) => name === 'cherries' );
console.log(result) // { name: 'cherries', quantity: 5 }
xxxxxxxxxx
const array1 = [5, 12, 8, 130, 44];
const found = array1.find(element => element > 10);
console.log(found);
// expected output: 12
xxxxxxxxxx
const myArray = ["sun","moon","earth"];
const lookingFor = "earth"
console.log(myArray.find(planet => { return planet === lookingFor })
// expected output: earth
xxxxxxxxxx
var ages = [3, 10, 18, 20];
function checkAdult(age) {
return age >= 18;
}
/* find() runs the input function agenst all array components
till the function returns a value
*/
ages.find(checkAdult);
xxxxxxxxxx
const inventory = [
{name: 'apples', quantity: 2},
{name: 'bananas', quantity: 0},
{name: 'cherries', quantity: 5}
];
function isCherries(fruit) {
return fruit.name === 'cherries';
}
console.log(inventory.find(isCherries)); // { name: 'cherries', quantity: 5 }
/* find মেথড অলওয়েজ অ্যারের সরাসরি ভ্যালু রিটার্ণ করে।
অর্থাৎ কন্ডিশনের সাথে মিলে যাওয়ার পরে যে কারণে মিলসে সেই লজিক অনুযায়ী
ঐ অ্যারে থেকে প্রথম ভ্যালুটা রিটার্ণ করে। সে কারণে এখানে অ্যারের পুরো ভ্যালুটা আউটপুট হিসেবে দেখাচ্ছে। */
// Same Code Using Arrow function & destructuring technique
const inventory = [
{name: 'apples', quantity: 2},
{name: 'bananas', quantity: 0},
{name: 'cherries', quantity: 5}
];
const result = inventory.find( ({ name }) => name === 'cherries' ); // ({name})==> destructuring technique
console.log(result); // { name: 'cherries', quantity: 5 }
xxxxxxxxxx
const users = [
{id: 1, name: "test user 1"},
{id: 2, name: "test user 2"}
];
const found = users.find(user => user.id === 1);
const notFound = users.find(user => user.id === 99)
console.log(found) // { id: 1, name: 'test user 1' }
console.log(notFound) // undefined
xxxxxxxxxx
function include(arr, obj) {
for (var i = 0; i < arr.length; i++) {
if (arr[i] == obj) return true;
}
}
console.log(include([1, 2, 3, 4], 3)); // true
console.log(include([1, 2, 3, 4], 6)); // undefined
Run code snippet
xxxxxxxxxx
const numbers = [4, 9, 16, 25, 29];
let first = numbers.find(myFunction);
function myFunction(value, index, array) {
return value > 18;
}
xxxxxxxxxx
The first element that will be found by that function
const f = array1.find(e => e > 10);