fix jshint issue, optimize code in loop