refactored private `_each` function to handle objects in addition to arrays