How to Filter a JavaScript Object?

Estimated read time 1 min read

You can filter a JavaScript object by using the Array.prototype.filter() method and converting the object to an array using Object.entries() or Object.keys() and then filtering the resulting array.

Here’s an example:

const originalObject = {a: 1, b: 2, c: 3, d: 4};

const filteredArray = Object.entries(originalObject)
  .filter(([key, value]) => value % 2 === 0)
  .reduce((obj, [key, value]) => {
    obj[key] = value;
    return obj;
  }, {});

console.log(filteredArray); 
// Output: {b: 2, d: 4}

You May Also Like

More From Author

+ There are no comments

Add yours

Leave a Reply