Write a Generic Reusable Filter Function (JavaScript)
viaLeetCode
Requirements: Implement your own generic, reusable filter function in JavaScript (similar to Array.prototype.filter) that accepts a predicate callback and returns a new array of elements for which the predicate returns true.
Design/Approach: Iterate over the input array, invoking the predicate with (element, index, array) for each item; push elements where the predicate returns truthy into a results array and return it. Consider edge cases such as sparse arrays and preserving the original array (no mutation).
asked …