Refactor the loop of Array.getElementByKey