CrackFrontendCF
Resources
Practice
CrackFrontend

Your guide to mastering frontend interviews

Resources

LibraryPracticeDonate

Company

About UsContact

Legal

Privacy PolicyTerms of Service

Built with ❀️ by Tushar Khanna

CrackFrontendCF
Resources
Practice

πŸ” Array.prototype.filter() Polyfill

The filter() method creates a new array with elements passing a test function. Critical for data filtering and commonly used in interviews.

The method creates a new array with all elements that pass the test implemented by the provided callback function. This polyfill replicates the native behavior, including support for and proper handling of sparse arrays. -- βœ… Implementation Example Usage: Key Features of This Polyfill: 1. Prototype Extension : The method is added to , making it available on all arrays. 1. Callback Execution : The callback function is executed on each array element, receiving as arguments. This allows the callback to inspect the element, index, and the entire array. 1. Handling : The parameter is used to bind a custom context when executing the callback function. 1. Sparse Array Handling : The method uses to ensure that only actual elements are processed. This prevents issues when working with sparse arrays or arrays with holes, ensuring only the array's own properties are considered (ignoring inherited properties). 1. Type Checking : The method checks if the argument is a valid function. If not, it throws a , mimicking the behavior of the native method. This polyfill behaves similarly to the native , allowing for array filtering based on a custom condition. It handles , ensures correct element processing in sparse arrays, and performs type checking on the callback. -- <!-quiz-start --Q1: What does return? [ ] The first matching element [ ] The original array modified in place [x] A new array with elements that pass the test [ ] The count of matching elements Q2: Why does the filter polyfill use ? [ ] To check if the callback exists [ ] To validate the index is a number [x] To handle sparse arrays and skip holes [ ] To ensure the element is not null Q3: What does return? [ ] undefined [ ] null [ ] [1, 2, 3] [x] [] <!-quiz-end --
JavaScriptPolyfills
🎯 Array.prototype.at() Polyfill
medium
βœ… Array.prototype.every() Polyfill
medium
πŸ”² Array.prototype.fill() Polyfill
medium
πŸ” Array.prototype.filter() Polyfill
medium
πŸ”Ž Array.prototype.find() Polyfill
medium
πŸ”’ Array.prototype.findIndex() Polyfill
medium
πŸ”™ Array.prototype.findLast() Polyfill
medium
πŸ”™ Array.prototype.findLastIndex() Polyfill
medium
πŸ“‹ Array.prototype.flat() Polyfill
medium
πŸ” Array.prototype.includes() Polyfill
medium
πŸ”’ Array.prototype.indexOf() Polyfill
medium
βœ… Array.isArray() Polyfill
medium
πŸ—ΊοΈ Array.prototype.map() Polyfill
hard
βž– Array.prototype.pop() Polyfill
medium
βž• Array.prototype.push() Polyfill
easy
πŸ”„ Array.prototype.reduce() Polyfill
medium
πŸ”„ Array.prototype.reverse() Polyfill
hard
⬅️ Array.prototype.shift() Polyfill
hard
πŸ”˜ Array.prototype.some() Polyfill
medium
πŸ”€ Array.prototype.sort() Polyfill
hard
➑️ Array.prototype.unshift() Polyfill
hard
πŸ“ž Function.prototype.apply() Polyfill
medium
πŸ”— Function.prototype.bind() Polyfill
hard
πŸ“ž Function.prototype.call() Polyfill
medium
4 of 24
LibraryJavaScriptPolyfills19 of 61

πŸ” Array.prototype.filter() Polyfill

jspolyfillsmedium

The filter() method creates a new array with all elements that pass the test implemented by the provided callback function. This polyfill replicates the native behavior, including support for thisArg and proper handling of sparse arrays.


βœ… Implementation

Array.prototype.myFilter = function(callback, thisArg) {
    if (typeof callback !== "function") {
        throw new TypeError(callback + " is not a function");
    }

    let result = [];

    for (let i = 0; i < this.length; i++) {
        if (this.hasOwnProperty(i)) { // Ensures only actual elements are processed
            if (callback.call(thisArg, this[i], i, this)) {
                result.push(this[i]);
            }
        }
    }

    return result;
};

Example Usage:

const numbers = [1, 2, 3, 4, 5, 6];
const evenNumbers = numbers.myFilter(num => num % 2 === 0);
console.log(evenNumbers); // Output: [2, 4, 6]

Key Features of This Polyfill:

  1. Prototype Extension :
  • The myFilter method is added to Array.prototype, making it available on all arrays.
  1. Callback Execution :
  • The callback function is executed on each array element, receiving (element, index, array) as arguments.
  • This allows the callback to inspect the element, index, and the entire array.
  1. Handling thisArg :
  • The thisArg parameter is used to bind a custom this context when executing the callback function.
  1. Sparse Array Handling :
  • The method uses this.hasOwnProperty(i) to ensure that only actual elements are processed.
  • This prevents issues when working with sparse arrays or arrays with holes, ensuring only the array's own properties are considered (ignoring inherited properties).
  1. Type Checking :
  • The method checks if the callback argument is a valid function.
  • If not, it throws a TypeError, mimicking the behavior of the native Array.prototype.filter method.

This polyfill behaves similarly to the native Array.prototype.filter, allowing for array filtering based on a custom condition. It handles thisArg, ensures correct element processing in sparse arrays, and performs type checking on the callback.


Quick Quiz

Test your understanding with 3 quick questions

Q1What does `filter()` return?
Q2Why does the filter polyfill use `this.hasOwnProperty(i)`?
Q3What does `[1, 2, 3].myFilter(x => x > 5)` return?

Continue Reading

CrackFrontend

Your guide to mastering frontend interviews

Resources

LibraryPracticeDonate

Company

About UsContact

Legal

Privacy PolicyTerms of Service

Built with ❀️ by Tushar Khanna