Javascript How To Iterate Through An Array Of Objects With Checking