Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
Selected Reading
How to create a custom function similar to find() method in JavaScript?
Let's say we have the following records of studentId and studentName and want to check a specific student name:
const studentDetails = [
{
studentId: 101,
studentName: "John"
},
{
studentId: 102,
studentName: "David"
},
{
studentId: 103,
studentName: "Carol"
}
];
Creating a custom function similar to JavaScript's built-in find() method allows you to implement custom search logic and understand how array searching works internally.
Custom Find Function Example
Here's a custom function that searches for a student by name:
const studentDetails = [
{
studentId: 101,
studentName: "John"
},
{
studentId: 102,
studentName: "David"
},
{
studentId: 103,
studentName: "Carol"
}
];
function findByName(name) {
for (let i = 0; i < studentDetails.length; i++) {
if (studentDetails[i].studentName === name) {
return studentDetails[i];
}
}
return undefined;
}
// Test the function
let result = findByName("David");
console.log("Found student:", result);
result = findByName("Mike");
console.log("Student not found:", result);
Found student: { studentId: 102, studentName: 'David' }
Student not found: undefined
Generic Custom Find Function
A more flexible approach is to create a generic find function that accepts a callback:
function customFind(array, callback) {
for (let i = 0; i < array.length; i++) {
if (callback(array[i], i, array)) {
return array[i];
}
}
return undefined;
}
// Usage examples
const students = [
{ studentId: 101, studentName: "John", age: 20 },
{ studentId: 102, studentName: "David", age: 22 },
{ studentId: 103, studentName: "Carol", age: 19 }
];
// Find by name
let foundStudent = customFind(students, student => student.studentName === "Carol");
console.log("Found by name:", foundStudent);
// Find by age
foundStudent = customFind(students, student => student.age > 21);
console.log("Found by age > 21:", foundStudent);
Found by name: { studentId: 103, studentName: 'Carol', age: 19 }
Found by age > 21: { studentId: 102, studentName: 'David', age: 22 }
Comparison with Built-in find()
| Feature | Custom Find | Built-in find() |
|---|---|---|
| Performance | Similar | Optimized |
| Flexibility | Full control | Standard behavior |
| Return Value | Customizable | Element or undefined |
Key Points
- Custom find functions return the first matching element, just like the built-in method
- Return
undefinedwhen no match is found for consistency - Use strict equality (
===) for more reliable comparisons - Generic implementations with callbacks provide maximum flexibility
Conclusion
Creating custom find functions helps you understand array searching mechanisms and provides flexibility for specific use cases. While built-in methods are typically preferred, custom implementations are valuable for learning and specialized requirements.
Advertisements
