When working with objects it is sometimes nice to quickly be able to make a custom object that is composed of properties from another object, just a few of them, not the whole thing. For this in lodash there is the _.pick method that can be used to create a new object that is a shallow copy of a given object, but with only properties that are in a given list of property names. So as the name suggests it is a pay to go about picking properties from another object, and create a new object with this list of properties from a source object.
So when it comes to just working with vanilla js by itself there is the delete operator. This can be used as a way to remove unwanted properties from an object, but it will of course mutate an existing object rather than create a new one. When using delete as a way to create a new object with a selection of properties from original object, and not mutate the original the original will have to be cloned first.
There is also the lodash omit method that works more or less the same way as the loadh pick method only it creates a new object from and old object by omitting properties that are not wanted. In this case the new object contains all properties that are not in the array of properties names rather than just the properties names in the give array.
For a basic example of using _.pick I will be giving a simple object that is created from the object literal notation. This object contains some simple primitives, and one property that is an Array. This will serve well as an example of what _.pick does, and also what to look out for when using it.
So here is that day object that will be used in some examples here in the remainder of this section. I have a property that contains the date of a day in a string format, along with additional properties for a user count for the day, pageviews, and a page property that contains a detailed breakdown of users and pageviews via an array of objects.
When dealing with an object like this, there might be scenarios where I might just want the date, and users properties. Also In some cases I might want the pages Array, and have it so it is a deep copy as well.
So if I want a new object that is just a shallow copy of some of the primitive values of the day object, _.pick works just fine out of the gate like this.
I have my object with just the properties that I want, without the rest of the clutter, great. However things can get a little tricky when it comes to properties that have values that are not primitives.
So if I get another new object that is the result of using _.pick, but this time include the pages array, some might think that this array is a copy, or clone of the original. It is not, anything non-primitive will be referenced in.
In some cases this is not a problem if I actually want references rather than a copy. However if it is a problem one solution is to just make a _.deepClone of the object, then passing that as the object for _.pick.
If I want the pages array to be a copy, rather than a reference then this is where methods like _.deepClone come into play.
When using _.pick a new object is going to be returned, so if I am using an object that has a constructor other than that of Object, then I am going to loose everything attached to the prototype. This is one of the main things to consider when doing anything with objects. Do I want a copy, or are references okay, and do I care about the prototype.
Say for example I have a Day constructor that can be used to create in instance of a day object rather than just an object literal.
This differs from my earlier example in that now I can create an object that has a prototype with it.
So after creating an instance of Day all my methods work just fine.
However if I use _.pick I will loose the prototype.
No big deal thought I would just need to make a new one.
I often just create a new object, and then reference in the properties that I want from a source object.
The thing to keep in mind here is that I am creating a new object, and in this example I am copying over primitive values. If I am copying over nested objects I can still run into problems with copying by reference, but the same is true with the lodash pick method by itself without using a deep clone method of one kind or another.
4.2 - Making a pick method with Object.keys, array filter, array some, forEach, and the a new Object
I can then have another array of picked key names that I want from the source object that is given as an argument. This array of picked names can be used in the body of my array filter function with the array some method called off of the array of picked keys, and in the function that I pass to array some I can make a comparison to see if this is one of the picked keys or not. The resulting array can then be used as a way to create the new object that contains the picked keys from the source object.