loading...
Cover image for Omit unwanted data from Object using JavaScript

Omit unwanted data from Object using JavaScript

rajnishkatharotiya profile image Rajnish Katharotiya ・3 min read

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

Posted on May 13 by:

rajnishkatharotiya profile

Rajnish Katharotiya

@rajnishkatharotiya

JavaScript enthusiast, Front-end developer, Blogger & YouTuber (https://www.youtube.com/channel/UCvNjso_gPQIPacA6EraoZmg)

Discussion

markdown guide
 

Sorry to be blunt, but this is a fairly useless example. If you're going to have to pass in a function to filter the keys you want, you might as well just write a simple mapping for the object you want instead.