The _.forIn lodash method will loop over all own, and inherited properties of an object. In other words the key value pairs that are specific to the object, as well as anything that is part of the objects prototype. Other options for loping over the contents of an object might just loop over what is an actual Constructor object instance property, rather than any additional properties that might be present in the prototype of the Constructor.
In this section I will be exploring ways to go about looping over both own and inherited properties just using vanilla js. When it comes to this I was not able to come up with many solutions aside from a for in loop. Other than that there are ways to make an array of key names and then use that to loop over all own and inherited properties in an object.
Just a plain old for in loop seems to work just fine to loop over own properties of an object as well as what is in the prototype object as well.
Another way would be to create an array of key names, and loop over that with Array.forEach, while loop ect. Using Object.keys will give me all the enumerable properties of an object, but not it’s prototype, unless I give the prototype object itself. So I could use Object.keys twice, and build an array of keys that way.
So _.forIn is useful for just quickly looping over both the own properties, as well as inherited properties of an Object. The only types of properties that it will not loop over are non enumerable properties. However that can be done in vanilla js with the help of Object.getOwnPropertyNames. I would get into that in detail as well but that is a matter for another post. If you enjoyed this post you might want to also check out mu posts on _.forOwn which is just like _.forIn only it loops of the own properties of an object. There is also my post on _.forEach a collection method that is another way of looping over key value pairs with lodash.