const numbers = [50, 60, 70, 80];
const element = numbers.find(num => num > 50);
console.log(element);
// -> 60
In the code snippet, We have an array named numbers that contains multiple integer values. We are trying to find the array element that has a value greater than 50. We are using the .find() method of javascript Array and applying the condition inside the function.
Live Demo
const names = ["Rick Grimes", "John Snow", "Carol", "Misshone"];
//Using ES syntax
const name_snow = names.find(name => name.indexOf("Snow") != -1);
console.log(name_snow);
// -> John Snow
// Using function
const name_snow_2 = names.find(function(name) {
return name.indexOf("rol") != -1
});
console.log(name_snow_2);
// -> Carol
const subjects = [
{"name": "Math", "score": 85},
{ "name": "Physics", "score": 90 },
{ "name": "Chemistry", "score": 95 }
];
const subject = subjects.find(sub => sub.score > 90);
console.log(subject);
// -> { "name": "Chemistry", "score": 95 }
0 Comments