
- 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
Implementing a custom function like Array.prototype.filter() function in JavaScript
Problem
We are required to write a JavaScript function that lives on the prototype Object of the Array class.
Our function should take in a callback function as the only argument. This callback function should be called for each element of the array.
And that callback function should take in two arguments the corresponding element and its index. If the callback function returns true, we should include the corresponding element in our output array otherwise we should exclude it.
Example
Following is the code −
const arr = [5, 3, 6, 2, 7, -4, 8, 10]; const isEven = num => num % 2 === 0; Array.prototype.customFilter = function(callback){ const res = []; for(let i = 0; i < this.length; i++){ const el = this[i]; if(callback(el, i)){ res.push(el); }; }; return res; }; console.log(arr.customFilter(isEven));
Output
[ 6, 2, -4, 8, 10 ]
- Related Articles
- Implementing custom function like String.prototype.split() function in JavaScript
- Implementing the Array.prototype.lastIndexOf() function in JavaScript
- Create a custom toLowerCase() function in JavaScript
- Writing a custom URL shortener function in JavaScript
- Implementing Math function and return m^n in JavaScript
- How to define custom sort function in JavaScript?
- Number prime test in JavaScript by creating a custom function?
- Implement a custom function similar to Array.prototype.includes() method using JavaScript
- Custom len() Function In Python
- How to create a custom function similar to find() method in JavaScript?
- Remove duplicate items from an array with a custom function in JavaScript
- Validate Date in MySQL using a custom function
- Applying a custom function to each corresponding element of two arrays using JavaScript
- Function returning another function in JavaScript
- Make a custom function call without using SAP NetWeaver

Advertisements