implements eachWithIndex() method and replace() method for array