First of all, it's considered bad practice to extend Object.prototype. Instead, provide your feature as stand-alone function, or if you really want to extend a global, provide it as utility function on Object, just like there already are Object.keys, Object.assign, Object.is, ...etc.

I provide here several solutions:

  1. Using reduce and Object.keys
  2. As (1), in combination with Object.assign
  3. Using map and spread syntax instead of reduce
  4. Using Object.entries and Object.fromEntries

1. Using reduce and Object.keys

With reduce and Object.keys to implement the desired filter (using ES6 arrow syntax):

Object.filter = (obj, predicate) => 
    Object.keys(obj)
          .filter( key => predicate(obj[key]) )
          .reduce( (res, key) => (res[key] = obj[key], res), {} );

// Example use:
var scores = {
    John: 2, Sarah: 3, Janet: 1
};
var filtered = Object.filter(scores, score => score > 1); 
console.log(filtered);

Note that in the above code predicate must be an inclusion condition (contrary to the exclusion condition the OP used), so that it is in line with how Array.prototype.filter works.

2. As (1), in combination with Object.assign

In the above solution the comma operator is used in the reduce part to return the mutated res object. This could of course be written as two statements instead of one expression, but the latter is more concise. To do it without the comma operator, you could use Object.assign instead, which does return the mutated object:

Object.filter = (obj, predicate) => 
    Object.keys(obj)
          .filter( key => predicate(obj[key]) )
          .reduce( (res, key) => Object.assign(res, { [key]: obj[key] }), {} );

// Example use:
var scores = {
    John: 2, Sarah: 3, Janet: 1
};
var filtered = Object.filter(scores, score => score > 1); 
console.log(filtered);

3. Using map and spread syntax instead of reduce

Here we move the Object.assign call out of the loop, so it is only made once, and pass it the individual keys as separate arguments (using the spread syntax):

Object.filter = (obj, predicate) => 
    Object.assign(...Object.keys(obj)
                    .filter( key => predicate(obj[key]) )
                    .map( key => ({ [key]: obj[key] }) ) );

// Example use:
var scores = {
    John: 2, Sarah: 3, Janet: 1
};
var filtered = Object.filter(scores, score => score > 1); 
console.log(filtered);

4. Using Object.entries and Object.fromEntries

As the solution translates the object to an intermediate array and then converts that back to a plain object, it would be useful to make use of Object.entries (ES2017) and the opposite (i.e. create an object from an array of key/value pairs) with Object.fromEntries (ES2019).

It leads to this "one-liner" method on Object:

Object.filter = (obj, predicate) => 
                  Object.fromEntries(Object.entries(obj).filter(predicate));

// Example use:
var scores = {
    John: 2, Sarah: 3, Janet: 1
};

var filtered = Object.filter(scores, ([name, score]) => score > 1); 
console.log(filtered);

The predicate function gets a key/value pair as argument here, which is a bit different, but allows for more possibilities in the predicate function's logic.

Answer from trincot on Stack Overflow
🌐
MDN Web Docs
developer.mozilla.org › en-US › docs › Web › JavaScript › Reference › Global_Objects › Array › filter
Array.prototype.filter() - JavaScript | MDN
The filter() method is generic. It only expects the this value to have a length property and integer-keyed properties. The following example uses filter() to create a filtered array that has all elements with values less than 10 removed.
🌐
W3Schools
w3schools.com › jsref › jsref_filter.asp
JavaScript Array filter() Method
The filter() method creates a new array filled with elements that pass a test provided by a function.
🌐
freeCodeCamp
freecodecamp.org › news › javascript-filter-method
How the JavaScript Filter Method Works – Explained with Code Examples
February 15, 2024 - The filter method in JavaScript is designed as a higher-order function that iterates over each element of an array, allowing developers to apply a specific condition to filter out elements.
🌐
Medium
medium.com › @benjamin.flanders96 › how-to-create-a-simple-javascript-filter-67e50b32ccb5
How to Create A Simple JavaScript Filter | Medium
July 22, 2020 - The very last thing we need to ... onto our filter objects according to what category they fit into. Please see the example image below. ... NOTE: Some objects may fit into multiple categories, like the Orange object. Orange is both a fruit and a color. In this case you will add multiple class names onto the object. Tada! Congratulations, you have just built out your very first JavaScript filter using ...
Top answer
1 of 16
474

First of all, it's considered bad practice to extend Object.prototype. Instead, provide your feature as stand-alone function, or if you really want to extend a global, provide it as utility function on Object, just like there already are Object.keys, Object.assign, Object.is, ...etc.

I provide here several solutions:

  1. Using reduce and Object.keys
  2. As (1), in combination with Object.assign
  3. Using map and spread syntax instead of reduce
  4. Using Object.entries and Object.fromEntries

1. Using reduce and Object.keys

With reduce and Object.keys to implement the desired filter (using ES6 arrow syntax):

Object.filter = (obj, predicate) => 
    Object.keys(obj)
          .filter( key => predicate(obj[key]) )
          .reduce( (res, key) => (res[key] = obj[key], res), {} );

// Example use:
var scores = {
    John: 2, Sarah: 3, Janet: 1
};
var filtered = Object.filter(scores, score => score > 1); 
console.log(filtered);

Note that in the above code predicate must be an inclusion condition (contrary to the exclusion condition the OP used), so that it is in line with how Array.prototype.filter works.

2. As (1), in combination with Object.assign

In the above solution the comma operator is used in the reduce part to return the mutated res object. This could of course be written as two statements instead of one expression, but the latter is more concise. To do it without the comma operator, you could use Object.assign instead, which does return the mutated object:

Object.filter = (obj, predicate) => 
    Object.keys(obj)
          .filter( key => predicate(obj[key]) )
          .reduce( (res, key) => Object.assign(res, { [key]: obj[key] }), {} );

// Example use:
var scores = {
    John: 2, Sarah: 3, Janet: 1
};
var filtered = Object.filter(scores, score => score > 1); 
console.log(filtered);

3. Using map and spread syntax instead of reduce

Here we move the Object.assign call out of the loop, so it is only made once, and pass it the individual keys as separate arguments (using the spread syntax):

Object.filter = (obj, predicate) => 
    Object.assign(...Object.keys(obj)
                    .filter( key => predicate(obj[key]) )
                    .map( key => ({ [key]: obj[key] }) ) );

// Example use:
var scores = {
    John: 2, Sarah: 3, Janet: 1
};
var filtered = Object.filter(scores, score => score > 1); 
console.log(filtered);

4. Using Object.entries and Object.fromEntries

As the solution translates the object to an intermediate array and then converts that back to a plain object, it would be useful to make use of Object.entries (ES2017) and the opposite (i.e. create an object from an array of key/value pairs) with Object.fromEntries (ES2019).

It leads to this "one-liner" method on Object:

Object.filter = (obj, predicate) => 
                  Object.fromEntries(Object.entries(obj).filter(predicate));

// Example use:
var scores = {
    John: 2, Sarah: 3, Janet: 1
};

var filtered = Object.filter(scores, ([name, score]) => score > 1); 
console.log(filtered);

The predicate function gets a key/value pair as argument here, which is a bit different, but allows for more possibilities in the predicate function's logic.

2 of 16
295

Never ever extend Object.prototype.

Horrible things will happen to your code. Things will break. You're extending all object types, including object literals.

Here's a quick example you can try:

    // Extend Object.prototype
Object.prototype.extended = "I'm everywhere!";

    // See the result
alert( {}.extended );          // "I'm everywhere!"
alert( [].extended );          // "I'm everywhere!"
alert( new Date().extended );  // "I'm everywhere!"
alert( 3..extended );          // "I'm everywhere!"
alert( true.extended );        // "I'm everywhere!"
alert( "here?".extended );     // "I'm everywhere!"

Instead create a function that you pass the object.

Object.filter = function( obj, predicate) {
    let result = {}, key;

    for (key in obj) {
        if (obj.hasOwnProperty(key) && !predicate(obj[key])) {
            result[key] = obj[key];
        }
    }

    return result;
};
🌐
MDN Web Docs
developer.mozilla.org › en-US › docs › Web › JavaScript › Reference › Global_Objects › Iterator › filter
Iterator.prototype.filter() - JavaScript | MDN
The following example creates an ... 1; let next = 1; while (true) { yield current; [current, next] = [next, current + next]; } } const seq = fibonacci().filter((x) => x % 2 === 0); console.log(seq.next().value); // 2 console.log(seq.next().value); // 8 console.log(seq.next().value); ...
🌐
DEV Community
dev.to › avinashrepo › 10-example-of-filter-using-in-js-5h6i
10 example of filter using in js - DEV Community
February 6, 2024 - In JavaScript, filtering arrays is a common operation achieved using the filter() method. This method... Tagged with javascript.
🌐
Built In
builtin.com › software-engineering-perspectives › javascript-filter
How to Filter a JavaScript Array With the Filter() Method | Built In
Assign the new filtered array to a new variable. Here is an example that uses the filter() method to filter an array based on a search criteria for car brands that start with the letter “B.”
Find elsewhere
🌐
JavaScript Tutorial
javascripttutorial.net › home › javascript array methods › array.prototype.filter()
JavaScript Array filter() Method
November 7, 2024 - As a result, the filter() method ... (javascript) The following example uses the filter() method returns an array of cities that have a population greater than 3 million: const cities = [ { name: 'Los Angeles', population: 3_792_621 ...
🌐
Flexiple
flexiple.com › javascript › javascript-filter-array
How to use the JavaScript filter array method? - Flexiple Tutorials - Flexiple
We use the JavaScript filter array function to return freelancers containing the particular skillset. Furthermore, the JavaScript filter does not mutate the string but rather creates a new one hence the original string is not affected.
🌐
DigitalOcean
digitalocean.com › community › tutorials › js-filter-array-method
How To Use the filter() Array Method in JavaScript | DigitalOcean
August 26, 2021 - Use filter() on arrays to go through an array and return a new array with the elements that pass the filtering rules.
🌐
Programiz
programiz.com › javascript › library › array › filter
Javascript Array filter()
// using arrow function const searchArr = (arr, query) => arr.filter(element => element.toLowerCase().indexOf(query.toLowerCase()) !== -1); let newLanguages = searchArr(languages, "p"); console.log(newLanguages); // [ 'JavaScript', 'Python', 'PHP' ]
🌐
Mimo
mimo.org › glossary › javascript › filter
Master JavaScript Filter Operator: Apply Filter Arrays
The filter() method takes a callback function that defines the condition for filtering the array elements. The callback function is called for each element of the array, and elements that return true are kept, while others are excluded. ... Become a full-stack developer. Learn HTML, CSS, JavaScript, and React as well as NodeJS, Express, and SQL
🌐
GeeksforGeeks
geeksforgeeks.org › javascript-array-filter-method
JavaScript Array filter() Method | GeeksforGeeks
January 10, 2025 - // JavaScript to illustrate findIndex() ... elements pass the test. Example 1: Creating a new array consisting of only those elements that satisfy the condition checked by isPositive() function....
🌐
MDN Web Docs
developer.mozilla.org › en-US › docs › Web › JavaScript › Reference › Global_Objects › TypedArray › filter
TypedArray.prototype.filter() - JavaScript | MDN
The following example uses filter() to create a filtered typed array that has all elements with values less than 10 removed. js · function isBigEnough(element, index, array) { return element >= 10; } new Uint8Array([12, 5, 8, 130, 44]).filter(isBigEnough); // Uint8Array [ 12, 130, 44 ] Polyfill ...
🌐
freeCodeCamp
freecodecamp.org › news › filter-arrays-in-javascript
How to Filter an Array in JavaScript – JS Filtering for Arrays and Objects
November 7, 2024 - In this article, you have learned how to filter an array in JavaScript using the for loop and filter() method.
🌐
Medium
medium.com › @jaysrisaravanan › javascript-filter-and-reduce-methods-3d03007b9e5e
JavaScript filter() and reduce() methods | by Jaysri Saravanan | Medium
September 24, 2023 - Let’s create an example where ... We’ll use the filter() method to select the foods with selected value as true, and then use the reduce() method to calculate the total cost of those selected products....
🌐
Simplilearn
simplilearn.com › home › resources › software development › javascript tutorial: learn javascript from scratch › how to use javascript array filter(): explained with examples
How to Use JavaScript Array Filter() With Examples | Simplilearn
August 11, 2025 - The JavaScript Array Filter() filters out the elements of an array based on the specified test condition. Read on to know the syntax and parameter values, how it works with example and the limitations.
Address   5851 Legacy Circle, 6th Floor, Plano, TX 75024 United States
🌐
LogRocket
blog.logrocket.com › home › how to use the array filter() method in javascript
How to use the array filter() method in JavaScript - LogRocket Blog
March 24, 2025 - This works because many JavaScript methods return an object, enabling consecutive method calls in a seamless flow. By chaining array methods, we can efficiently perform complex transformations while keeping the code concise and readable. Before diving into chaining, let’s quickly review how these methods work individually: filter() – Narrows down the array by selecting only elements that meet a specific condition
🌐
DEV Community
dev.to › mursalfk › javascript-filter-method-1gl3
JavaScript .filter() Method 💭 - DEV Community
July 4, 2021 - Fun example: Because "classes" are functions, .filter(Boolean).length is magic for, for example, evaluating composed validation rules. ... The JavaScript Arrays are something that I "get" when explained to me, but I have a hard time really ingraining the concepts.