
- Javascript Basics Tutorial
- Javascript - Home
- Javascript - Overview
- Javascript - Syntax
- Javascript - Enabling
- Javascript - Placement
- Javascript - Variables
- Javascript - Operators
- Javascript - If...Else
- Javascript - Switch Case
- Javascript - While Loop
- Javascript - For Loop
- Javascript - For...in
- Javascript - Loop Control
- Javascript - Functions
- Javascript - Events
- Javascript - Cookies
- Javascript - Page Redirect
- Javascript - Dialog Boxes
- Javascript - Void Keyword
- Javascript - Page Printing
- JavaScript Objects
- Javascript - Objects
- Javascript - Number
- Javascript - Boolean
- Javascript - Strings
- Javascript - Arrays
- Javascript - Date
- Javascript - Math
- Javascript - RegExp
- Javascript - HTML DOM
- JavaScript Advanced
- Javascript - Error Handling
- Javascript - Validations
- Javascript - Animation
- Javascript - Multimedia
- Javascript - Debugging
- Javascript - Image Map
- Javascript - Browsers
- JavaScript Useful Resources
- Javascript - Questions And Answers
- Javascript - Quick Guide
- Javascript - Functions
- Javascript - Resources
JavaScript function that lives on the prototype object of the Array class
Problem
We are required to write a JavaScript function that lives on the prototype object of the Array class. This function should take in a callback function and this function should return that very first element for which the callback function yields true.
We should feed the current element and the current index to the callback function as first and second argument.
Example
Following is the code −
const arr = [4, 67, 24, 87, 15, 78, 3]; Array.prototype.customFind = function(callback){ for(let i = 0; i < this.length; i++){ const el = this[i]; if(callback(el, i)){ return el; }; continue; }; return undefined; }; console.log(arr.customFind(el => el % 5 === 0));
Output
15
- Related Articles
- Adding a function for swapping cases to the prototype object of strings - JavaScript
- JavaScript Array prototype Constructor
- How to create an object with prototype in JavaScript?
- Importance of function prototype in C
- How to access a JavaScript object using its own prototype?
- Filter the properties of an object based on an array and get the filtered object JavaScript
- Sort object array based on another array of keys - JavaScript
- What is the purpose of a function prototype in C/C++?
- Function that returns the minimum and maximum value of an array in JavaScript
- How do I write a function that takes an array of values and returns an object JavaScript?
- Accessing variables in a constructor function using a prototype method with JavaScript?
- Filter an object based on an array JavaScript
- How does JavaScript .prototype work?
- Currified function that multiples array elements in JavaScript
- Manipulate Object to group based on Array Object List in JavaScript

Advertisements