lodash object methods and beyond
In lodash methods there are a number of Object methods on top of array methods, and collection methods. When it comes to array method these kinds of method will just work with an array that is given as the source object, while collection methods will work with any kind of collection not just arrays. Although it might be true that collection methods are also a kind of object method there are still methods that are designed to work with an object that is a collection of items.
When working with a collection in one form or another I would want to loop over the own properties of a collection object only. So then maybe a good way of knowing the difference between collection methods would be to take into account the nature of the lodash forEach collection method, and compare that to the lodash forIn object method. The forEach method will just loop over the public own properties of an object, while the forIn method will loop over all the own properties, and also the prototype object of the object as well.
1 - The basics of object methods, collections, and arrays
1.1 - The Object define property method
1.2 - A Constructor function
1.3 - The lodash for each and for in methods
So now that I got some basic things out of the way when it comes to private and public keys, as well as how to go about having own properties and inherited properties I think I can now compare two lodash methods, one of which is a collection method and the other is an object method. The two methods I will be looking at in this example are the forEach and forIn methods. The forEach method will just loop over the public own properties of an object, which is the kind of behaviors that I would want when it comes to working with a collection. With any kind of collection each public own property should be some kind of element, item, or whatever one will call it, and these are generally the only values I want to work with when it comes to such objects. The for in object method however will not just loop over the public own properties, but will also loop over the public keys of the prototype as well. This is not what I would typically want to happen when working with a collection, but in some cases I might want to do this when it comes to working with objects in general.
2 - lodash object methods
For example in lodash there is the _.assign method
However there is also the native Object.assign methods as well now that works more or less the same way.
2.1 - Lodash merge and lodash assign
The lodash assign and merge methods are two good examples of lodash object methods. They both do more or less the same thing but with just some note worthy differences. The merge lodash object methods will recursively merge down own and inherited object properties while the lodash assign will just assign properties. Also the merge method will skip source object properties that will evaluate to undefined, while assign will not skip them and just assign everything in the source objects.
3 - lodash array object methods
4 - lodash collection object methods
5 - Conclusion
So then lodash has a wide range of useful object methods that can be used to work with any kind of object, not just a specific class of object such as an array, or methods that will just work well with an object that is still a kind of collection. There is still a great deal more that I could and should write about when it comes to this topic, but I only have so much time to do so when it comes to writing new posts, as well as editing older posts. I do get around to editing my content on lodash once in a while, but it might be a long time until I get around to this post again. In the mean time if you enjoyed this post thus far there is checking out another one of my lodash posts maybe of which are on the topic of a given objects or collection method in lodash.