replace imperative loops involving arrays mutation with Array.map/mapi