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
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 › Array › filter
Array.prototype.filter() - JavaScript | MDN
December 13, 2025 - The filter() method of Array instances creates a shallow copy of a portion of a given array, filtered down to just the elements from the given array that pass the test implemented by the provided function.
Discussions

Implement a Matching Object Filter - Implement a Matching Object Filter
In addition to my last reply. I noticed why my program isn’t returning an empty array for test cases. For the majority of the test cases, if the property existed in both objects then the value always matched up. There was no difference. However, in these last two test cases the values are ... More on forum.freecodecamp.org
🌐 forum.freecodecamp.org
3 weeks ago
Return object from filter when one of the values matches
Some cards have multiple category descriptions. I am trying to filter the results based off the select options. I am having trouble selecting any objects that contain the categories in the dropdown. I am easily able to do this if it only has one category, but if it was multiple it will not work. More on forum.freecodecamp.org
🌐 forum.freecodecamp.org
1
0
August 24, 2023
What's the best way to filter out object parameters with undefined/null/empty string values ?
You can iterate keys on descendants of Object via Object.entries. This lets you handle potential values how you want when you construct your return object. https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/entries More on reddit.com
🌐 r/learnjavascript
11
6
December 16, 2023
How to filter array of object keys against an array of strings
You guys are awesome! Thank you so much. Non-Toxic and so much better than Stack Overflow. I've never gotten nearly as much support there as I have here. More on reddit.com
🌐 r/learnjavascript
22
12
May 1, 2023
🌐
MuleSoft
docs.mulesoft.com › dataweave › latest › dw-core-functions-filterobject
filterObject | MuleSoft Documentation
This example outputs an object if its value equals "apple". %dw 2.0 output application/json --- {"a" : "apple", "b" : "banana"} filterObject ((value) -> value == "apple")
🌐
Adobe
helpx.adobe.com › photoshop user guide › create and manage layers › smart objects - overview and benefits
Smart Objects - overview and benefits
1 week ago - Edit filters without losing quality: Apply filters to preserve the original image and edit them whenever you need. Update all linked copies: Edit one Smart Object to automatically update all linked instances.
Find elsewhere
🌐
Adobe
helpx.adobe.com › photoshop user guide › effects and filters › blend and fade filter effects
Blend and fade filter effects in Photoshop
1 week ago - Convert embedded Smart Objects to linked · Filter the Layers panel by Smart Objects · Duplicate an embedded Smart Object · Edit the contents of a Smart Object · Replace the contents of a Smart Object · Convert Smart Objects to layers · Rasterize Smart Objects ·
🌐
W3Schools
w3schools.com › jsref › jsref_filter.asp
W3Schools.com
The filter() method creates a new array filled with elements that pass a test provided by a function.
🌐
freeCodeCamp
forum.freecodecamp.org › javascript
Implement a Matching Object Filter - Implement a Matching Object Filter
3 weeks ago - In addition to my last reply. I noticed why my program isn’t returning an empty array for test cases. For the majority of the test cases, if the property existed in both objects then the value always matched up. There was no difference. However, in these last two test cases the values are different for the same property name. function whatIsInAName(objArr, sourceObj) { const newArray = []; // Array to be returned let elementMatch; console.log("Array of Objects:"); console.log(obj...
🌐
Stack Abuse
stackabuse.com › how-to-filter-an-object-by-key-in-javascript
How to Filter an Object by Key in JavaScript
March 17, 2022 - In this short article - we've taken a look at filtering objects by value, using the Object.keys() method, filtered via the filter() method.
🌐
Mastering JS
masteringjs.io › tutorials › fundamentals › filter-object
How to Filter an Object with JavaScript - Mastering JS
December 21, 2020 - const obj = { name: 'Luke Skywalker', title: 'Jedi Knight', age: 23 }; // Convert `obj` to a key/value array // `[['name', 'Luke Skywalker'], ['title', 'Jedi Knight'], ...]` const asArray = Object.entries(obj); const filtered = asArray.filter(([key, value]) => typeof value === 'string'); // Convert the key/value array back to an object: // `{ name: 'Luke Skywalker', title: 'Jedi Knight' }` const justStrings = Object.fromEntries(filtered);
🌐
Palantir
palantir.com › docs › foundry › quiver › objects-filter
Quiver • Objects • Filter object sets • Palantir
The most common way to filter objects in Quiver is to use the filter object set card. Hover over any object set card to display the next actions menu below the card.
🌐
30 Seconds of Code
30secondsofcode.org › home › javascript › object › filter object keys or values
Filter a JavaScript object's properties by keys or values - 30 seconds of code
January 14, 2024 - For example, we might want to filter out all the properties whose values are numbers. This isn't particularly difficult to implement, either. We'll simply replace the check for the array inclusion with a call to the provided predicate function. In order to make the API of our function more user-friendly, the predicate should expect the value as the first argument and the key as the second one. const pickBy = (obj, fn) => Object.fromEntries(Object.entries(obj).filter(([k, v]) => fn(v, k))); const omitBy = (obj, fn) => Object.fromEntries(Object.entries(obj).filter(([k, v]) => !fn(v, k))); const obj = { a: 1, b: '2', c: 3 }; pickBy(obj, x => typeof x === 'number'); // { a: 1, c: 3 } omitBy(obj, x => typeof x !== 'number'); // { a: 1, c: 3 }
🌐
GeeksforGeeks
geeksforgeeks.org › javascript › how-to-implement-a-filter-for-objects-in-javascript
Implement a filter() for Objects in JavaScript - GeeksforGeeks
October 14, 2025 - In JavaScript, objects don’t have a built-in filter() method like arrays, but you can implement similar functionality manually.
🌐
Scott Spence
scottspence.com › home › posts › reduce and filter javascript object on property
Reduce and Filter JavaScript Object on Property - Scott Spence
January 25, 2022 - How do you filter a JavaScript object that has duplicate properties? Use reduce! The specific situation I had is a list of objects with dupl...