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 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.
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.
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.