DEV Community

Cover image for Omit unwanted data from Object using JavaScript
Rajnish Katharotiya
Rajnish Katharotiya

Posted on

Omit unwanted data from Object using JavaScript

Photo by S O C I A L . C U T on Unsplash

Remove particular data from an object is still an easy task to do. But guess you want to remove all data which not matched with your condition like an example, you want only positive values from the object and omit all negative once, how you'll do it?

Before going further, I would like to welcome you to a new episode of series call Javascript Useful Snippets. In this series, I'm sharing some shortcodes and useful functions that can let you make your code faster and neat. So, if you haven't read my previous episodes' articles please check it out here or else stay tuned till the end to learn something new 😋 .

Also check out my youtube channel for video tutorials : Subscribe me to support 🙏

How to Omit unwanted key-value pairs from the object?

Guess, you have an object which has values in the data type of number and string and you only want numbers. So, in cases like this you can use this custom javascript function called omitBy(). This javascript snippet will take two arguments first one will be your object and second will be your prediction (in our case value should be in number). And in result, it'll return an object with only number values with it's key. Let's take a look at the function:-

How omitBy() function works ?

const omitBy = (obj, fn) =>
  Object.keys(obj)
    .filter(k => !fn(obj[k], k))
    .reduce((acc, key) => ((acc[key] = obj[key]), acc), {});

Here In function, I've first created an array of all keys of an object by using Object.keys() method. Once I have a collection of keys, I've executed the filter method on the array to filter out keys that don't satisfy the given function. And by using a reduced method, I've created a new collection of all keys which returned after filter method execution. So, as output, we will have an object with all key-value pairs which didn't match without given function or except those value which matched with our function.

How to use omitBy() function ?

omitBy({ a: 1, b: '2', c: 3 }, x => typeof x !== 'number');  // Output :- { a: 1, c: 3 }

As we talked above, here I've passed one object which content string and number type of values and out of all we want only those pair which content number values. So, I've added function in the second argument which checks if type of x shouldn't be number. So, in return as we see we have objects with omitted pairs by a given function.

This, helped me a lot to optimize objects before passing down to trees in development. So, I thought to share it with you guys too. I hope you liked my explanation (if yes, hit like ❤️ button ) and if you found it informative then do follow from here because I'll learn and share every day.😋

Also follow/subscribe me on my social media account to connect with me : twitter, youtube

Top comments (1)

Some comments may only be visible to logged-in visitors. Sign in to view all comments.