Animations
API
Arrays
Async
Basics
Challenges
Classes
Console
Dates
Debugging
DOM Elements
DOM Methods
DOM Properties
Event Listeners
Flow Control
Forms
Functions
Global Functions
JSON
Keywords
Libraries (3rd party)
Math
Modules
Objects
Snippets
String
Types
Widgets
Window Object
https://www.w3schools.com/jsref/jsref_obj_array.asp
The find()
returns the value of the first element in an array that pass a test (provided as a function).
The find()
method executes the function once for each element present in the array.
If it finds an array element where the function returns a true value,
find()
returns the value of that array element (and does not check the remaining values)
Otherwise it returns undefined
.
find()
does not execute the function for empty arrays.
find()
does not change the original array.
$(function ()
{
let clarkeMatch = authors.find((author) => { return author.name.toLowerCase() === "arthur c clarke" });
$("#demo1").html(clarkeMatch.name + "<br />Books: " + clarkeMatch.books.join(", "));
// or
//let clarkeMatch = authors.find(GetArthur);
//$("#demo1").html(clarkeMatch.name + "<br />Books: " + clarkeMatch.books.join(", "));
//const GetArthur = () => { return author.name.toLowerCase() === "arthur c clarke"};
});
You can use an in-line function as show below or reference (name only) the name of another full function.