Omit unwanted data from Object using JavaScript
Tags: javascript, codenewbie, node, webdev
Published: 2020-05-24 11:24:29

*Photo by [S O C I A L . C U T](https://unsplash.com/@socialcut?utm_source=unsplash&utm_medium=referral&utm_content=creditCopyText) on [Unsplash](https://unsplash.com/s/photos/code-girl?utm_source=unsplash&utm_medium=referral&utm_content=creditCopyText)*

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](https://dev.to/rajnishkatharotiya) or else stay tuned till the end to learn something new 😋 .

*Also check out my youtube channel for video tutorials : [Subscribe me to support 🙏](https://www.youtube.com/channel/UCvNjso_gPQIPacA6EraoZmg?sub_confirmation=1)*

## 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](https://dev.to/rajnishkatharotiya) because I'll learn and share every day.😋

Also follow/subscribe me on my social media account to connect with me : [twitter](https://twitter.com/RajnishK00), [youtube](https://www.youtube.com/channel/UCvNjso_gPQIPacA6EraoZmg?sub_confirmation=1)