Syntax refactor for looping an array