You could switch this to a DB key by doing:

  key = mediaArr[idx].key.toString.replace(/ /g, '_');