refractoring javascript array passing from controller