Using Lodash’s omit() Function – onlinecode
In this post, we will give you information about Using Lodash’s omit() Function. Here we will give you detail about Using Lodash’s omit() Function And how to use it also give you a demo for it if it is necessary.
Given an object obj
and an array of strings paths
, Lodash’s pick()
function returns a new object
with all the keys in obj
that are not in paths
.
omit()
is the opposite of Lodash’s pick()
function.
const obj = {
name: 'Will Riker',
rank: 'Commander',
age: 29
};
const newObj = _.omit(obj, ['rank', 'age']);
newObj === obj; // false
newObj.name; // 'Will Riker'
newObj.rank; // undefined
newObj.age; // undefined
omit()
will not throw any errors if you try to omit paths that don’t exist.
const obj = {
name: 'Will Riker',
rank: 'Commander',
age: 29
};
// Works fine even though 'something else' isn't a path in 'obj'
const newObj = _.omit(obj, ['rank', 'age', 'something else']);
newObj === obj; // false
newObj.name; // 'Will Riker'
newObj.rank; // undefined
newObj.age; // undefined
Dotted Paths for Using Lodash’s omit() Function
Like pick()
, omit()
supports dotted paths and other syntaxes that Lodash’s get()
function supports.
You can omit properties underneath objects, and even individual array elements.
const obj = {
name: {
first: 'Will',
last: 'Riker'
},
rank: 'Commander',
ships: ['USS Enterprise', 'USS Pegasus'],
age: 29
};
const newObj = _.omit(obj, ['name.first', 'age', 'ships.1']);
newObj === obj; // false
newObj.name.first; // undefined
newObj.name.last; // 'Riker'
newObj.rank; // 'Commander'
newObj.ships; // ['USS Enterprise',] Note that index 1 is an array hole
newObj.age; // undefined
Vanilla JS Alternatives for Using Lodash’s omit() Function
If all you want to do is omit some top-level keys from an object, you might not need Lodash.
There are several ways to replicate omit()
without Lodash, excluding dotted paths and other advanced functionality.
The most concise way is to use an anonymous arrow function combined with the spread operator as follows.
const obj = {
name: 'Will Riker',
rank: 'Commander',
age: 29
};
// Looks tricky, but the idea is that you're calling an anonymous arrow
// function that uses the spread operator to get all properties _other than_
// 'rank' and 'age'.
const newObj = (({ rank, age, ...rest }) => rest)(obj);
newObj === obj; // false
newObj.name; // 'Will Riker'
newObj.rank; // undefined
newObj.age; // undefined
The above approach is concise, but hard to read, especially for beginners.
You can also implement a short helper function as follows.
function omit(obj, keys) {
const ret = {};
keys = new Set(keys);
const keysToCopy = Object.keys(obj).filter(key => !keys.has(key));
for (const key of keysToCopy) {
ret[key] = obj[key];
}
return ret;
}
Lodash is a JavaScript library which provides utility functions for common programming tasks using the functional programming paradigm. Lodash is a JavaScript library that helps programmers write more concise and maintainable JavaScript. It can be broken down into several main areas: Utilities: for simplifying common programming tasks such as determining type as well as simplifying math operations.
Why Lodash?
Lodash makes JavaScript easier by taking the hassle out of working with arrays, numbers, objects, strings, etc.Lodash’s modular methods are great for:
- Iterating arrays, objects, & strings
- Manipulating & testing values
- Creating composite functions
Hope this code and post will helped you for implement Using Lodash’s omit() Function. if you need any help or any feedback give it in comment section or you have good idea about this post you can give it comment section. Your comment will help us for help you more and improve us. we will give you this type of more interesting post in featured also so, For more interesting post and code Keep reading our blogs