How To Avoid Nested Loops Javascript