Lodash’s `pick()` Function – onlinecode
In this post, we will give you information about Lodash’s `pick()` Function – onlinecode. Here we will give you detail about Lodash’s `pick()` Function – onlinecode 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 just the keys paths
from obj
.
const obj = {
name: 'Will Riker',
rank: 'Commander',
age: 29
};
const picked = _.pick(obj, ['name', 'rank']);
picked === obj; // false
picked.name; // 'Will Riker'
picked.rank; // 'Commander'
picked.age; // undefined
The pick()
function also supports dotted paths and any other syntax that
Lodash’s get()
function supports. For example,
if name
is a nested object with 2 properties, first
and last
, you
can pick just name.last
and omit name.first
.
const obj = {
name: {
first: 'Will',
last: 'Riker'
},
rank: 'Commander',
age: 29
};
const picked = _.pick(obj, ['name.last', 'rank']);
picked === obj; // false
picked.name.first; // undefined
picked.name.last; // 'Riker'
picked.rank; // 'Commander'
picked.age; // undefined
pick()
is permissive when it comes to missing properties. If you try
to pick()
a dotted property whose parent is undefined, Lodash will
just ignore that path.
const obj = {
name: 'Will Riker',
rank: 'Commander',
age: 29
};
// Lodash will ignore 'this.is.not.in.the.object', because
// that path isn't in the object.
const picked = _.pick(obj, ['name', 'this.is.not.in.the.object']);
picked === obj; // false
picked.name; // 'Will Riker'
picked.rank; // undefined
picked.age; // undefined
Several other frameworks have analogous pick()
functions. For example,
Mongoose schemas have a pick()
function that creates a new schema a subset of the original schema’s paths.
Vanilla JS Alternatives
There are a couple of different patterns you can use to approximate Lodash’s pick()
function in vanilla JavaScript.
Our favorite is to use an IIFE as shown below:
const obj = {
name: 'Will Riker',
rank: 'Commander',
age: 29
};
// Looks tricky, but the idea is that you're calling an anonymous arrow
// function that returns just the 'name' and 'rank' properties of the passed
// in object.
const picked = (({ name, rank }) => ({ name, rank }))(obj);
picked.name; // 'Will Riker'
picked.rank; // 'Commander'
picked.age; // undefined
Below is an easier to read, but less concise, way of doing the same thing:
const pickNameRank = ({ name, rank }) => ({ name, rank });
const picked = pickNameRank(obj);
This IIFE approach has a few downsides:
- You need to know the property names ahead of time. It would be tricky to extend this pattern to operate on an array of keys.
- No support for dotted keys. You can’t do something like
({ 'name.first' }) => ...
.
Another approach we like is defining a function with a for
loop that creates a new object as shown below.
This approach is less concise, but more flexible.
function pick(obj, keys) {
const ret = {};
for (const key of keys) {
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’s `pick()` Function – onlinecode)?
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 Lodash’s `pick()` Function – onlinecode. 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