refactored private `_each` function to handle objects as well as arrays