Iterating Through Object Literals in JavaScript
Garrett Levine
42

Nice article, thanks! I’d just add a small notes:

The `for…in` iterates over all enumerable properties — even inherited one. This may cause some confusion sometimes. Often it is combined with `hasOwnProperty` check … and that makes it complicated and not nice.

Maybe I’d add that using `forEach` (with `Object.keys`) should be done for scenarios when you need to perform side effects (`forEach` doesn’t return nothing). Using `map`, `filter`, `reduce` and others returns (new — viva immutability) array and therefore allows you to compose and reuse.

It’s not unusual that you can use utility function to transform object to array of [key, value] arrays, then perform whatever you need (cleanly and functionally) and in the end you can reduce to object again, if needed.

But I’m fan (and layman) of functional programming, thus I prefer this approach.