The lodash forown method
Looping over all keys in an object is something that comes up a whole lot when working on a javScript project. There are a wide range of ways of doing so with arrays, such as the array for each method, or using a while loop. However there is also all kinds of ways of doing so with objects in general also, not just arrays, but array like objects, and various kinds of objects that are collections in the form of named rather than index keys. So then there are ways of creating an array of key names for objects in general, and then looping over the resulting array as a way to do so with such objects.
Sometimes it would be nice to have a method that will only loop over key value pares that are actually part of the object rather than values that are inherited from the prototype of the objects class. One option to do so is the _.forOwn method in lodash that can be used as a way to loop over all the own properties of an object. There is also of course the _.forIn method as well that will loop over the own properties of an object as well as what is in the prototype object of the object on top of that.
1 - Lodash for own basic examples, and what to know first
1.1 - A Basic example of _.forOwn
For a basic example of _.forOwn I put together a quick example that involves a custom made constructor method, and a prototype for that method. The _.forOwn method will loop over just the own properties of the object, and will not loop over anything in the prototype object of the constructor that was used to create the object.
1.2 - lodash for in
The lodash _.forIn method on the other hand will loop over both the own properties as well as the inherited properties.
2.1 - Using a for in loop
So _.forOwn is one of those methods in lodash where I am scratching my head wondering why I should bother with lodash, because doing things like this is not that difficult in vanilla js. The hasOwnProperty Object prototype method can be used as a way to find out if the property of an object is the own property of that object rather than an inherited property for example. There is also the native for in loop that will loop over all the properties of an object just like that of the lodash for in method, but I can just use this has own property method in the body of the for in loop to check if it is an own property or not.
So the for in loop will loop over the own properties of an object as well inherited properties, and the hasOwnProperty method has to be used as a way to filter those out, and just have the own properties of the object. The end result is something that is more or less the same as what happens when using the lodash for own method. However maybe I should over at least a few more examples of this sort of thing in order to explore this at least a little farther as there are additional options that come to mind.
2.2 - Using the get Own Property Names static object method
Another option would be the get own property names method that will return an array of key names that are own properties of an object. This array can then be looped over using a method like the array for each method, or another other options for doing so such as a while loop, or one of the many other array prototype methods.
2.3 - Creating a method
3 - Conclusion
If you enjoyed this post be sure to check out my other posts on lodash, there is also the main post on lodash that I have written that I get to editing often as I keep running threw some of these older posts now and then. If you have any questions or concerns feel free to drop a line in the comments section in this post.